natural: Natural number

[ bsd3, control, data, library ] [ Propose Tags ] [ Report a vulnerability ]

Natural number with lens integration.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
dev

Enable development warnings (-Werror, -O2 for benchmarks)

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.3.0.2, 0.3.0.3, 0.3.0.4, 0.3.0.5, 0.3.0.6, 0.3.0.7, 0.4.0.0, 0.5.0.1
Change log changelog.md
Dependencies aeson (>=1.4 && <3), base (>=4.8 && <6), lens (>=4.15 && <6), semigroupoids (>=5 && <7) [details]
Tested with ghc ==9.6.7
License BSD-3-Clause
Author Tony Morris <tmorris@tmorris.net>
Maintainer Tony Morris <tmorris@tmorris.net>
Uploaded by TonyMorris at 2026-05-28T00:35:52Z
Category Data
Home page https://gitlab.com/system-f/code/natural
Bug tracker https://gitlab.com/system-f/code/natural/-/issues
Source repo head: git clone https://gitlab.com/system-f/code/natural.git
Distributions
Reverse Dependencies 1 direct, 2 indirect [details]
Downloads 4440 total (62 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2026-05-28 [all 1 reports]

Readme for natural-0.5.0.1

[back to package description]

natural

Safe natural number, positive integer, and non-zero integer types with lens integration.

Types

Type Range Semigroup Monoid identity
Natural >= 0 addition 0
Positive >= 1 multiplication
NotZero /= 0 addition

Each type has corresponding newtype wrappers for alternative semigroups:

Wrapper Operation
ProductNatural multiplication
MaxNatural / MinNatural max / min
SumPositive addition
MaxPositive / MinPositive max / min
SumNotZero addition
MaxNotZero / MinNotZero max / min

Optics

The library uses lens for type-safe conversions:

-- Prisms for safe construction from integral types
(5 :: Integer) ^? _Natural   -- Just (Natural 5)
(-1 :: Integer) ^? _Natural  -- Nothing

(3 :: Integer) ^? _Positive  -- Just (Positive 3)
(0 :: Integer) ^? _Positive  -- Nothing

(7 :: Integer) ^? _NotZero   -- Just (NotZero True (Positive 7))
(0 :: Integer) ^? _NotZero   -- Nothing

-- Structural prisms
Natural 5 ^? successor       -- Just (Natural 4)
Natural 0 ^? successor       -- Nothing

Positive 3 ^? successor1     -- Just (Positive 2)
Positive 1 ^? successor1     -- Nothing

-- Isos between related types
Natural 4 ^. successorW      -- Positive 5
Natural 3 ^. naturalPositive -- Just (Positive 3)
Natural 3 ^. list            -- [(), (), ()]

Type classes

Each type has Has* (lens) and As* (prism) classes with instances for standard integral types (Int, Integer, Word, Const, Identity):

class HasNatural a where
  natural :: Lens' a Natural

class AsNatural a where
  _Natural :: Prism' a Natural

NotZero

A non-zero integer represented as a sign (Bool: True = positive) and a magnitude (Positive):

data NotZero = NotZero Bool Positive

positiveNotZero (Positive 5)  -- NotZero True (Positive 5)
negativeNotZero (Positive 3)  -- NotZero False (Positive 3)
notZeroInteger (NotZero False (Positive 3))  -- -3

-- Multiplication is always total
multiplyNZ (NotZero False (Positive 3)) (NotZero False (Positive 4))
  -- NotZero True (Positive 12)

-- Addition can produce zero
plusNZ (NotZero True (Positive 3)) (NotZero False (Positive 3))
  -- Nothing

Building

cabal build
cabal test doctest
cabal bench