Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Haskus.Utils.Types.Nat
Contents
Description
Type-level Nat
Synopsis
- type Nat = Natural
- natValue :: forall (n :: Nat) a. (KnownNat n, Num a) => a
- natValue' :: forall (n :: Nat). KnownNat n => Word
- class KnownNat (n :: Nat)
- data SomeNat = KnownNat n => SomeNat (Proxy n)
- someNatVal :: Natural -> SomeNat
- sameNat :: forall (a :: Nat) (b :: Nat) proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 b -> Maybe (a :~: b)
- type family CmpNat (a :: Natural) (b :: Natural) :: Ordering where ...
- type (<=?) (m :: k) (n :: k) = OrdCond (Compare m n) 'True 'True 'False
- type (<=) (x :: t) (y :: t) = Assert (x <=? y) (LeErrMsg x y :: Constraint)
- 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 :: Natural) + (b :: Natural) :: Natural where ...
- type family (a :: Natural) - (b :: Natural) :: Natural where ...
- type family (a :: Natural) * (b :: Natural) :: Natural where ...
- type family (a :: Natural) ^ (b :: Natural) :: Natural where ...
- type family Mod (a :: Natural) (b :: Natural) :: Natural where ...
- type family Log2 (a :: Natural) :: Natural where ...
- type family Div (a :: Natural) (b :: Natural) :: Natural where ...
- type family NatBitCount (n :: Nat) :: Nat where ...
Documentation
A type synonym for Natural
.
Prevously, this was an opaque data type, but it was changed to a type synonym.
Since: base-4.16.0.0
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
Minimal complete definition
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 :: forall (a :: Nat) (b :: Nat) proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 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 :: Natural) (b :: Natural) :: Ordering where ... #
Comparison of type-level naturals, as a function.
Since: base-4.7.0.0
type (<=?) (m :: k) (n :: k) = OrdCond (Compare m n) 'True 'True 'False infix 4 #
Comparison (<=) of comparable types, as a function.
Since: base-4.16.0.0
type (<=) (x :: t) (y :: t) = Assert (x <=? y) (LeErrMsg x y :: Constraint) infix 4 #
Comparison (<=) of comparable types, as a constraint.
Since: base-4.16.0.0
Operations
type family (a :: Natural) + (b :: Natural) :: Natural where ... infixl 6 #
Addition of type-level naturals.
Since: base-4.7.0.0
type family (a :: Natural) - (b :: Natural) :: Natural where ... infixl 6 #
Subtraction of type-level naturals.
Since: base-4.7.0.0
type family (a :: Natural) * (b :: Natural) :: Natural where ... infixl 7 #
Multiplication of type-level naturals.
Since: base-4.7.0.0
type family (a :: Natural) ^ (b :: Natural) :: Natural where ... infixr 8 #
Exponentiation of type-level naturals.
Since: base-4.7.0.0
type family Mod (a :: Natural) (b :: Natural) :: Natural 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 :: Natural) :: Natural 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 :: Natural) (b :: Natural) :: Natural 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
Equations
NatBitCount 0 = 1 | |
NatBitCount n = NatBitCount' (n + 1) (Log2 (n + 1)) |