Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Type-level Nat
Synopsis
- data Nat
- natValue :: forall (n :: Nat) a. (KnownNat n, Num a) => a
- natValue' :: forall (n :: Nat). KnownNat n => Word
- class KnownNat (n :: Nat)
- data SomeNat where
- someNatVal :: Natural -> SomeNat
- sameNat :: (KnownNat a, KnownNat b) => Proxy a -> Proxy b -> Maybe (a :~: b)
- type family CmpNat (a :: Nat) (b :: Nat) :: Ordering where ...
- type family (a :: Nat) <=? (b :: Nat) :: Bool where ...
- type (<=) (x :: Nat) (y :: Nat) = (x <=? y) ~ True
- type family NatEq a b :: Nat where ...
- type family NatNotEq a b :: Nat where ...
- type family Max (a :: Nat) (b :: Nat) where ...
- type family Min (a :: Nat) (b :: Nat) where ...
- type family IsZero (n :: Nat) :: Nat where ...
- type family IsNotZero (n :: Nat) :: Nat where ...
- type family (a :: Nat) + (b :: Nat) :: Nat where ...
- type family (a :: Nat) - (b :: Nat) :: Nat where ...
- type family (a :: Nat) * (b :: Nat) :: Nat where ...
- type family (a :: Nat) ^ (b :: Nat) :: Nat where ...
- type family Mod (a :: Nat) (b :: Nat) :: Nat where ...
- type family Log2 (a :: Nat) :: Nat where ...
- type family Div (a :: Nat) (b :: Nat) :: Nat where ...
- type family NatBitCount (n :: Nat) :: Nat where ...
Documentation
This class gives the integer associated with a type-level natural. There are instances of the class for every concrete literal: 0, 1, 2, etc.
Since: base-4.7.0.0
natSing
This type represents unknown type-level natural numbers.
Since: base-4.10.0.0
someNatVal :: Natural -> SomeNat #
Convert an integer into an unknown type-level natural.
Since: base-4.10.0.0
sameNat :: (KnownNat a, KnownNat b) => Proxy a -> Proxy b -> Maybe (a :~: b) #
We either get evidence that this function was instantiated with the
same type-level numbers, or Nothing
.
Since: base-4.7.0.0
Comparisons
type family CmpNat (a :: Nat) (b :: Nat) :: Ordering where ... #
Comparison of type-level naturals, as a function.
Since: base-4.7.0.0
type family (a :: Nat) <=? (b :: Nat) :: Bool where ... infix 4 #
Comparison of type-level naturals, as a function.
NOTE: The functionality for this function should be subsumed
by CmpNat
, so this might go away in the future.
Please let us know, if you encounter discrepancies between the two.
type (<=) (x :: Nat) (y :: Nat) = (x <=? y) ~ True infix 4 #
Comparison of type-level naturals, as a constraint.
Since: base-4.7.0.0
Operations
type family (a :: Nat) + (b :: Nat) :: Nat where ... infixl 6 #
Addition of type-level naturals.
Since: base-4.7.0.0
type family (a :: Nat) - (b :: Nat) :: Nat where ... infixl 6 #
Subtraction of type-level naturals.
Since: base-4.7.0.0
type family (a :: Nat) * (b :: Nat) :: Nat where ... infixl 7 #
Multiplication of type-level naturals.
Since: base-4.7.0.0
type family (a :: Nat) ^ (b :: Nat) :: Nat where ... infixr 8 #
Exponentiation of type-level naturals.
Since: base-4.7.0.0
type family Mod (a :: Nat) (b :: Nat) :: Nat where ... infixl 7 #
Modulus of natural numbers.
Mod x 0
is undefined (i.e., it cannot be reduced).
Since: base-4.11.0.0
type family Log2 (a :: Nat) :: Nat where ... #
Log base 2 (round down) of natural numbers.
Log 0
is undefined (i.e., it cannot be reduced).
Since: base-4.11.0.0
type family Div (a :: Nat) (b :: Nat) :: Nat where ... infixl 7 #
Division (round down) of natural numbers.
Div x 0
is undefined (i.e., it cannot be reduced).
Since: base-4.11.0.0
Helpers
type family NatBitCount (n :: Nat) :: Nat where ... Source #
Number of bits (>= 1) required to store a Nat value
>>>
natValue' @(NatBitCount 0)
1
>>>
natValue' @(NatBitCount 1)
1
>>>
natValue' @(NatBitCount 2)
2
>>>
natValue' @(NatBitCount 5)
3
>>>
natValue' @(NatBitCount 15)
4
>>>
natValue' @(NatBitCount 16)
5
NatBitCount 0 = 1 | |
NatBitCount n = NatBitCount' (n + 1) (Log2 (n + 1)) |