HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulefin-0.3.2Haskell2010

Data.Type.Nat

Nat numbers. DataKinds stuff.

This module re-exports Data.Nat, and adds type-level things.

  • 12 types
  • 1 class
  • 33 values
  • Packagefin-0.3.2
  • Exports54
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNat.hs

Natural, Nat numbers

4 declarations
datadata Nat
#

Nat natural numbers.

Better than GHC's built-in Nat for some use cases.

Constructors

Instances25Enum, Eq, Integral, Data, Num, Ord, …
  • Enum NatDefined in fin-0.3.2 · Data.Nat
  • Eq NatDefined in fin-0.3.2 · Data.Nat
  • Integral NatDefined in fin-0.3.2 · Data.Nat
  • Data NatDefined in fin-0.3.2 · Data.Nat
  • Num NatDefined in fin-0.3.2 · Data.Nat
  • Ord NatDefined in fin-0.3.2 · Data.Nat
  • Real NatDefined in fin-0.3.2 · Data.Nat
  • Show NatDefined in fin-0.3.2 · Data.Nat

    Nat is printed as Natural.

    To see explicit structure, use explicitShow or explicitShowsPrec

  • NFData NatDefined in fin-0.3.2 · Data.Nat
  • Arbitrary NatDefined in fin-0.3.2 · Data.Nat
  • CoArbitrary NatDefined in fin-0.3.2 · Data.Nat
  • Function NatDefined in fin-0.3.2 · Data.Nat
  • Hashable NatDefined in fin-0.3.2 · Data.Nat
  • Universe NatDefined in fin-0.3.2 · Data.Nat
    Example2 expressions
    import qualified Data.Universe.Class as Utake 10 (U.universe :: [Nat])[0,1,2,3,4,5,6,7,8,9]
  • Category LEProofDefined in fin-0.3.2 · Data.Type.Nat.LE.ReflStep

    The other variant (Data.Type.Nat.LE.LEPRoof) isn't Category, because leRefl requires SNat evidence.

  • TestEquality SNatDefined in fin-0.3.2 · Data.Type.Nat
  • EqP FinDefined in fin-0.3.2 · Data.Fin
    Example1 expression
    eqp FZ FZTrue
    Example1 expression
    eqp FZ (FS FZ)False
    Example1 expression
    let xs = universe @N.Nat4; ys = universe @N.Nat6 in traverse_ print [ [ eqp x y | y <- ys ] | x <- xs ][True,False,False,False,False,False][False,True,False,False,False,False][False,False,True,False,False,False][False,False,False,True,False,False]
  • EqP SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GCompare SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GEq SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GNFData SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GShow FinDefined in fin-0.3.2 · Data.Fin
  • GShow SNatDefined in fin-0.3.2 · Data.Type.Nat
  • OrdP FinDefined in fin-0.3.2 · Data.Fin
    Example1 expression
    let xs = universe @N.Nat4; ys = universe @N.Nat6 in traverse_ print [ [ comparep x y | y <- ys ] | x <- xs ][EQ,LT,LT,LT,LT,LT][GT,EQ,LT,LT,LT,LT][GT,GT,EQ,LT,LT,LT][GT,GT,GT,EQ,LT,LT]
  • OrdP SNatDefined in fin-0.3.2 · Data.Type.Nat
valuetoNatural :: Nat -> Natural
#

Convert Nat to Natural

Example1 expression
toNatural 00
Example1 expression
toNatural 22
Example1 expression
toNatural $ S $ S $ Z2
valuefromNatural :: Natural -> Nat
#

Convert Natural to Nat

Example1 expression
fromNatural 44
Example1 expression
explicitShow (fromNatural 4)"S (S (S (S Z)))"
valuecata :: a -> (a -> a) -> Nat -> a
#

Fold Nat.

Example1 expression
cata [] ('x' :) 2"xx"

Showing

2 declarations
valueexplicitShow :: Nat -> String
#

show displaying a structure of Nat.

Example1 expression
explicitShow 0"Z"
Example1 expression
explicitShow 2"S (S Z)"

Singleton

4 declarations
datadata SNat (n :: Nat) where
#

Singleton of Nat.

Constructors

Instances12TestEquality, EqP, GCompare, GEq, GNFData, GShow, …
  • TestEquality SNatDefined in fin-0.3.2 · Data.Type.Nat
  • EqP SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GCompare SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GEq SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GNFData SNatDefined in fin-0.3.2 · Data.Type.Nat
  • GShow SNatDefined in fin-0.3.2 · Data.Type.Nat
  • OrdP SNatDefined in fin-0.3.2 · Data.Type.Nat
  • Eq (SNat a)Defined in fin-0.3.2 · Data.Type.Nat
  • Ord (SNat a)Defined in fin-0.3.2 · Data.Type.Nat
  • Show (SNat p)Defined in fin-0.3.2 · Data.Type.Nat
  • NFData (SNat n)Defined in fin-0.3.2 · Data.Type.Nat
  • SNatI n => Boring (SNat n)Defined in fin-0.3.2 · Data.Type.Nat
patternpattern SS' :: () => m ~ 'S n => SNat n -> SNat m
#

A pattern with explicit argument

Example2 expressions
let predSNat :: SNat (S n) -> SNat n; predSNat (SS' n) = npredSNat (SS' (SS' SZ))SS
Example1 expression
reflect $ predSNat (SS' (SS' SZ))1
valuesnatToNatural :: SNat n -> Natural
#

Convert SNat to Natural

Example1 expression
snatToNatural (snat :: SNat Nat0)0
Example1 expression
snatToNatural (snat :: SNat Nat2)2

Implicit

6 declarations
classclass SNatI (n :: Nat) where
#

Implicit SNat.

In an unorthodox singleton way, it actually provides an induction function.

The induction should often be fully inlined. See test/Inspection.hs.

Example3 expressions
:set -XPolyKindsnewtype Const a b = Const a deriving (Show)induction (Const 0) (coerce ((+2) :: Int -> Int)) :: Const Int Nat3Const 6

Methods

Instances2SNatI
  • SNatI 'ZDefined in fin-0.3.2 · Data.Type.Nat
  • SNatI n => SNatI ('S n)Defined in fin-0.3.2 · Data.Type.Nat
valuereify :: Nat -> (forall (n :: Nat). SNatI n => Proxy n -> r) -> r
#

Reify Nat.

Example1 expression
reify nat3 reflect3

Equality

4 declarations
valueeqNat :: (SNatI n, SNatI m) => Maybe (n :~: m)
#

Decide equality of type-level numbers.

Example1 expression
eqNat :: Maybe (Nat3 :~: Plus Nat1 Nat2)Just Refl
Example1 expression
eqNat :: Maybe (Nat3 :~: Mult Nat2 Nat2)Nothing
valuediscreteNat :: (SNatI n, SNatI m) => Dec (n :~: m)
#

Decide equality of type-level numbers.

Example1 expression
decShow (discreteNat :: Dec (Nat3 :~: Plus Nat1 Nat2))"Yes Refl"
valuecmpNat :: (SNatI n, SNatI m) => GOrdering n m
#

Decide equality of type-level numbers.

Example1 expression
cmpNat :: GOrdering Nat3 (Plus Nat1 Nat2)GEQ
Example1 expression
cmpNat :: GOrdering Nat3 (Mult Nat2 Nat2)GLT
Example1 expression
cmpNat :: GOrdering Nat5 (Mult Nat2 Nat2)GGT

Induction

1 declaration
valueinduction1
  1. :: SNatI n
  2. => f 'Z a

    zero case

  3. -> (forall (m :: Nat). SNatI m => f m a -> f ('S m) a)

    induction step

  4. -> f n a
#

Induction on Nat, functor form. Useful for computation.

Example: unfoldedFix

valueunfoldedFix :: SNatI n => proxy n -> (a -> a) -> a
#

Unfold n steps of a general recursion.

Note: Always benchmark. This function may give you both bad properties: a lot of code (increased binary size), and worse performance.

For known n unfoldedFix will unfold recursion, for example

unfoldedFix (Proxy :: Proxy Nat3) f = f (f (f (fix f)))

Arithmetic

4 declarations
familytype family Plus (n :: Nat) (m :: Nat) :: Nat where
#

Addition.

Example1 expression
reflect (snat :: SNat (Plus Nat1 Nat2))3

Equations

familytype family Mult (n :: Nat) (m :: Nat) :: Nat where
#

Multiplication.

Example1 expression
reflect (snat :: SNat (Mult Nat2 Nat3))6

Equations

familytype family Mult2 (n :: Nat) :: Nat where
#

Multiplication by two. Doubling.

Example1 expression
reflect (snat :: SNat (Mult2 Nat4))8

Equations

familytype family DivMod2 (n :: Nat) :: (Nat, Bool) where
#

Division by two. False is 0 and True is 1 as a remainder.

Example1 expression
:kind! DivMod2 Nat7 == '(Nat3, True)DivMod2 Nat7 == '(Nat3, True) :: Bool= 'True
Example1 expression
:kind! DivMod2 Nat4 == '(Nat2, False)DivMod2 Nat4 == '(Nat2, False) :: Bool= 'True

Equations

Conversion to GHC Nat

2 declarations
familytype family ToGHC (n :: Nat) :: Nat where
#

Convert to GHC Nat.

Example1 expression
:kind! ToGHC Nat5ToGHC Nat5 :: GHC.Nat...= 5

Equations

familytype family FromGHC (n :: Nat) :: Nat where
#

Convert from GHC Nat.

Example1 expression
:kind! FromGHC 7FromGHC 7 :: Nat= 'S ('S ('S ('S ('S ('S ('S 'Z))))))

Equations

Aliases

0 declarations

Nat

promoted Nat

Proofs

6 declarations