type-level-numbers-0.1.0.4: Type level numbers implemented using type families.

Portabilityunportable (GHC only)
Stabilityunstable
MaintainerAlexey Khudyakov <alexey.skladnoy@gmail.com>
Safe HaskellNone

TypeLevel.Number.Nat

Contents

Description

This is type level natural numbers. They are represented using binary encoding which means that reasonable large numbers could be represented. With default context stack depth (20) maximal number is 2^18-1 (262143).

 Z           = 0
 I Z         = 1
 O (I Z)     = 2
 I (I Z)     = 3
 O (O (I Z)) = 4
 ...

It's easy to see that representation for each number is not unique. One could add any numbers of leading zeroes:

 I Z = I (O Z) = I (O (O Z)) = 1

In order to enforce uniqueness of representation only numbers without leading zeroes are members of Nat type class. This means than types are equal if and only if numbers are equal.

Natural numbers support comparison and following operations: Next, Prev, Add, Sub, Mul. All operations on numbers return normalized numbers.

Interface type classes are reexported from TypeLevel.Number.Classes

Synopsis

Natural numbers

data I n Source

One bit.

Instances

Nat (I n) => Show (I n) 
Nat (I n) => Positive (I n) 
Nat (I n) => NonZero (I n) 
Nat (I n) => Nat (O (I n)) 
Nat (I Z) 
Nat (O n) => Nat (I (O n)) 
Nat (I n) => Nat (I (I n)) 
Nat (I n) => Reify (I n) Int64 
Nat (I n) => Reify (I n) Int32 
(Nat (I n), Lesser (I n) (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (I Z))))))))))))))))) => Reify (I n) Int16 
(Nat (I n), Lesser (I n) (O (O (O (O (O (O (O (I Z))))))))) => Reify (I n) Int8 
Nat (I n) => Reify (I n) Word64 
Nat (I n) => Reify (I n) Word32 
(Nat (I n), Lesser (I n) (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (I Z)))))))))))))))))) => Reify (I n) Word16 
(Nat (I n), Lesser (I n) (O (O (O (O (O (O (O (O (I Z)))))))))) => Reify (I n) Word8 
Nat (I n) => Reify (I n) Int 
Nat (I n) => Reify (I n) Integer 

data O n Source

Zero bit.

Instances

Nat (O n) => Show (O n) 
Nat (O n) => Positive (O n) 
Nat (O n) => NonZero (O n) 
Number_Is_Denormalized Z => Nat (O Z) 
Nat (O n) => Nat (O (O n)) 
Nat (I n) => Nat (O (I n)) 
Nat (O n) => Nat (I (O n)) 
Nat (O n) => Reify (O n) Int64 
Nat (O n) => Reify (O n) Int32 
(Nat (O n), Lesser (O n) (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (I Z))))))))))))))))) => Reify (O n) Int16 
(Nat (O n), Lesser (O n) (O (O (O (O (O (O (O (I Z))))))))) => Reify (O n) Int8 
Nat (O n) => Reify (O n) Word64 
Nat (O n) => Reify (O n) Word32 
(Nat (O n), Lesser (O n) (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (I Z)))))))))))))))))) => Reify (O n) Word16 
(Nat (O n), Lesser (O n) (O (O (O (O (O (O (O (O (I Z)))))))))) => Reify (O n) Word8 
Nat (O n) => Reify (O n) Int 
Nat (O n) => Reify (O n) Integer 

data Z Source

Bit stream terminator.

Instances

class Nat n whereSource

Type class for natural numbers. Only numbers without leading zeroes are members of this type class.

Methods

toInt :: Integral i => n -> iSource

Convert natural number to integral value. It's not checked whether value could be represented.

Instances

Nat Z 
Number_Is_Denormalized Z => Nat (O Z) 
Nat (O n) => Nat (O (O n)) 
Nat (I n) => Nat (O (I n)) 
Nat (I Z) 
Nat (O n) => Nat (I (O n)) 
Nat (I n) => Nat (I (I n)) 

Template haskell utilities

Here is usage example for natT:

 n123 :: $(natT 123)
 n123 = undefined

natT :: Integer -> TypeQSource

Create type for natural number.

nat :: Integer -> ExpQSource

Create value for type level natural. Value itself is undefined.