| Copyright | (c) 2018-2021 Iris Ward |
|---|---|
| License | BSD3 |
| Maintainer | aditu.venyhandottir@gmail.com |
| Stability | experimental |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Data.TypeLits
Description
This module provides the same interface as GHC.TypeLits, but with
naming conflicts resolved in favour of this package. For example,
(<=) resolves to the kind-polymorphic version from Data.TypeNums.
If you are only working with type-level numbers, import Data.TypeNums instead. This module is purely for convenience for those who want to use both functionality from GHC.TypeLits and functionality from Data.TypeNums.
Synopsis
- type Nat = Natural
- class KnownNat (n :: Nat)
- natVal :: forall (n :: Nat) proxy. KnownNat n => proxy n -> Integer
- natVal' :: forall (n :: Nat). KnownNat n => Proxy# n -> Integer
- data SomeNat = KnownNat n => SomeNat (Proxy n)
- someNatVal :: Integer -> Maybe SomeNat
- sameNat :: forall (a :: Nat) (b :: Nat) proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 b -> Maybe (a :~: b)
- data TInt
- class KnownInt (n :: k)
- intVal :: forall {k} (n :: k) proxy. KnownInt n => proxy n -> Integer
- intVal' :: forall {k} (n :: k). KnownInt n => Proxy# n -> Integer
- data SomeInt = KnownInt n => SomeInt (Proxy n)
- someIntVal :: Integer -> SomeInt
- data Rat = k :% Nat
- class KnownRat (r :: k)
- ratVal :: forall {k} proxy (r :: k). KnownRat r => proxy r -> Rational
- ratVal' :: forall {k} (r :: k). KnownRat r => Proxy# r -> Rational
- data SomeRat = KnownRat r => SomeRat (Proxy r)
- someRatVal :: Rational -> SomeRat
- type family Simplify (x :: Rat) :: Rat where ...
- type family (a :: k1) ==? (b :: k2) :: Bool where ...
- type (/=?) (a :: k1) (b :: k2) = Not (a ==? b)
- type family (a :: k1) <=? (b :: k2) :: Bool where ...
- type (==) (a :: k1) (b :: k2) = (a ==? b) ~ 'True
- type (/=) (a :: k1) (b :: k2) = (a ==? b) ~ 'False
- type (<=) (a :: k1) (b :: k2) = (a <=? b) ~ 'True
- type (<) (a :: k1) (b :: k2) = (b <=? a) ~ 'False
- type (>=) (a :: k1) (b :: k2) = (b <=? a) ~ 'True
- type (>) (a :: k1) (b :: k2) = (a <=? b) ~ 'False
- type family Abs (x :: k) :: k where ...
- type family Negate (x :: k) :: NegK k where ...
- type family Recip (x :: k) :: Rat where ...
- type family Floor (x :: k) :: TInt where ...
- type family Ceiling (x :: k) :: TInt where ...
- type family Truncate (x :: k) :: TInt where ...
- type (+) (a :: k1) (b :: k2) = Add a b
- type (-) (a :: k1) (b :: k2) = Sub a b
- type (*) (a :: k1) (b :: k2) = Mul a b
- type (/) (a :: k1) (b :: k2) = RatDiv a b
- type (^) (a :: k1) (b :: k2) = Exp a b
- type family DivMod (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ...
- type family QuotRem (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ...
- type family Div (x :: k) (y :: Nat) :: IntDivK k where ...
- type family Mod (x :: k) (y :: Nat) :: IntDivK k where ...
- type family Quot (x :: k) (y :: Nat) :: IntDivK k where ...
- type family Rem (x :: k) (y :: Nat) :: IntDivK k where ...
- type family GCD (x :: k1) (y :: k2) :: Nat where ...
- type family IntLog (n :: Nat) (x :: k) :: TInt where ...
- type Log2 (x :: k) = IntLog 2 x
- data Symbol
- type family AppendSymbol (a :: Symbol) (b :: Symbol) :: Symbol where ...
- type family CmpSymbol (a :: Symbol) (b :: Symbol) :: Ordering where ...
- class KnownSymbol (n :: Symbol)
- symbolVal :: forall (n :: Symbol) proxy. KnownSymbol n => proxy n -> String
- symbolVal' :: forall (n :: Symbol). KnownSymbol n => Proxy# n -> String
- data SomeSymbol = KnownSymbol n => SomeSymbol (Proxy n)
- someSymbolVal :: String -> SomeSymbol
- sameSymbol :: forall (a :: Symbol) (b :: Symbol) proxy1 proxy2. (KnownSymbol a, KnownSymbol b) => proxy1 a -> proxy2 b -> Maybe (a :~: b)
- type family TypeError (a :: ErrorMessage) :: b where ...
- data ErrorMessage
Type level numbers
Naturals
A type synonym for Natural.
Previously, 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 :: Integer -> Maybe SomeNat #
Convert an integer into an unknown type-level natural.
Since: base-4.7.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
Integers
(Kind) An integer that may be negative.
class KnownInt (n :: k) Source #
This class gives the (value-level) integer associated with a type-level
integer. There are instances of this class for every concrete natural:
0, 1, 2, etc. There are also instances of this class for every negated
natural, such as .Neg 1
Minimal complete definition
intSing
intVal :: forall {k} (n :: k) proxy. KnownInt n => proxy n -> Integer Source #
Get the value associated with a type-level integer
This type represents unknown type-level integers.
Since: 0.1.1
someIntVal :: Integer -> SomeInt Source #
Convert an integer into an unknown type-level integer.
Since: 0.1.1
Rationals
Type constructor for a rational
Instances
class KnownRat (r :: k) Source #
This class gives the (value-level) rational associated with a type-level rational. There are instances of this class for every combination of a concrete integer and concrete natural.
Minimal complete definition
ratSing
Instances
| KnownInt n => KnownRat (n :: k) Source # | |
Defined in Data.TypeNums.Rats Methods ratSing :: SRat n | |
| (KnownInt n, KnownNat d, d /= 0) => KnownRat (n ':% d :: Rat) Source # | |
Defined in Data.TypeNums.Rats | |
| (TypeError ('Text "Denominator must not equal 0") :: Constraint) => KnownRat (n ':% 0 :: Rat) Source # | |
Defined in Data.TypeNums.Rats | |
ratVal :: forall {k} proxy (r :: k). KnownRat r => proxy r -> Rational Source #
Get the value associated with a type-level rational
This type represents unknown type-level integers.
Since: 0.1.1
someRatVal :: Rational -> SomeRat Source #
Convert a rational into an unknown type-level rational.
Since: 0.1.1
type family Simplify (x :: Rat) :: Rat where ... Source #
Reduce a type-level rational into its canonical form
Since: 0.1.4
Type level numerical operations
Comparison
type family (a :: k1) ==? (b :: k2) :: Bool where ... infix 4 Source #
Boolean type-level equals. Useful for e.g.
If (x ==? 0)
Equations
| (a :: Rat) ==? (b :: Rat) = Simplify a == Simplify b | |
| (a :: Nat) ==? ('Pos a :: TInt) = 'True | |
| ('Pos a :: TInt) ==? (a :: Nat) = 'True | |
| (a :: Nat) ==? (r :: Rat) = (a ':% 1) ==? r | |
| (r :: Rat) ==? (a :: Nat) = r ==? (a ':% 1) | |
| (a :: TInt) ==? (r :: Rat) = (a ':% 1) ==? r | |
| (r :: Rat) ==? (a :: TInt) = r ==? (a ':% 1) | |
| (a :: k) ==? (b :: k) = a == b | |
| (_1 :: k1) ==? (_2 :: k2) = 'False |
type family (a :: k1) <=? (b :: k2) :: Bool where ... infix 4 Source #
Boolean comparison of two type-level numbers
Equations
| (a :: Nat) <=? (b :: Nat) = a <=? b | |
| ('Pos a :: TInt) <=? ('Pos b :: TInt) = a <=? b | |
| ('Neg _1 :: TInt) <=? ('Pos _2 :: TInt) = 'True | |
| ('Pos 0) <=? ('Neg 0) = 'True | |
| ('Pos _1 :: TInt) <=? ('Neg _2 :: TInt) = 'False | |
| ('Pos a :: TInt) <=? (b :: k2) = a <=? b | |
| (a :: k1) <=? ('Pos b :: TInt) = a <=? b | |
| 0 <=? ('Neg 0) = 'True | |
| (a :: Nat) <=? ('Neg _1 :: TInt) = 'False | |
| ('Neg a :: TInt) <=? (b :: Nat) = 'True | |
| ('Neg a :: TInt) <=? ('Neg b :: TInt) = b <=? a | |
| (n ':% 0 :: Rat) <=? (_1 :: k2) = TypeError ('Text "Denominator must not equal 0") :: Bool | |
| (_1 :: k1) <=? (n ':% 0 :: Rat) = TypeError ('Text "Denominator must not equal 0") :: Bool | |
| (n1 ':% d1 :: Rat) <=? (n2 ':% d2 :: Rat) = (n1 * d2) <=? (n2 * d1) | |
| (a :: k1) <=? (n ':% d :: Rat) = (a * d) <=? n | |
| (n ':% d :: Rat) <=? (b :: k3) = n <=? (b * d) | |
| (_1 :: k1) <=? (_2 :: k2) = TypeError ('Text "Incomparable") :: Bool |
type (==) (a :: k1) (b :: k2) = (a ==? b) ~ 'True infix 4 Source #
Equality constraint, used as e.g. (x == 3) => _
Arithmetic
Unary operations
type family Abs (x :: k) :: k where ... Source #
The absolute value of a type-level number
Since: 0.1.4
type family Recip (x :: k) :: Rat where ... Source #
The reciprocal of a type-level number
Since: 0.1.4
type family Floor (x :: k) :: TInt where ... Source #
Round a type-level number towards negative infinity
Since: 0.1.4
type family Ceiling (x :: k) :: TInt where ... Source #
Round a type-level number towards positive infinity
Since: 0.1.4
type family Truncate (x :: k) :: TInt where ... Source #
Round a type-level number towards zero
Since: 0.1.4
Binary operations
type (-) (a :: k1) (b :: k2) = Sub a b infixl 6 Source #
The difference of two type-level numbers
For the difference of two naturals a and b, a-b is also a natural,
so only exists for a >= b.
type (*) (a :: k1) (b :: k2) = Mul a b infixl 7 Source #
The product of two type-level numbers.
Due to changes in GHC 8.6, using this operator infix and unqualified requires the NoStarIsType language extension to be active. See the GHC 8.6.x migration guide for details: https://ghc.haskell.org/trac/ghc/wiki/Migration/8.6
type (^) (a :: k1) (b :: k2) = Exp a b infixr 8 Source #
A type-level number raised to an integer power. For Nat powers, the
result kind is the same as the base. For TInt powers, the result kind
is Rat.
Since: 0.1.4
type family DivMod (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ... Source #
The quotient and remainder of a type-level integer and a natural number. For a negative dividend, the remainder part is positive such that x = q*y + r @since 0.1.4
Equations
| DivMod (_1 :: k) 0 = TypeError ('Text "Divisor must not be 0") :: (IntDivK k, IntDivK k) | |
| DivMod (x :: Nat) y = UnPos (DivModAux ('Pos x) y ('Pos 0)) :: (Nat, Nat) | |
| DivMod ('Pos x :: TInt) y = DivModAux ('Pos x) y ('Pos 0) | |
| DivMod ('Neg x :: TInt) y = DivModNegFixup (DivModAux ('Pos x) y ('Pos 0)) y |
type family QuotRem (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ... Source #
The quotient and remainder of a type-level integer and a natural number. For a negative dividend, the remainder part is negative such that x = q*y + r @since 0.1.4
type family Div (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The quotient of a type-level integer and a natural number.
Since: 0.1.4
type family Mod (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The remainder of a type-level integer and a natural number
For a negative number, behaves similarly to mod.
@since 0.1.4
type family Quot (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The integer part of the result of dividing an integer by a natural number
Since: 0.1.4
type family Rem (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The remainder of the result of dividing an integer by a natural number
Since: 0.1.4
type family GCD (x :: k1) (y :: k2) :: Nat where ... Source #
The greatest common divisor of two type-level integers
Since: 0.1.4
type family IntLog (n :: Nat) (x :: k) :: TInt where ... Source #
The floor of the logarithm of a type-level number
NB. unlike Log2, Log n 0 here is a type error.
Since: 0.1.4
Equations
| IntLog 0 (_1 :: k) = TypeError ('Text "Invalid IntLog base: 0") :: TInt | |
| IntLog 1 (_1 :: k) = TypeError ('Text "Invalid IntLog base: 1") :: TInt | |
| IntLog _1 0 = TypeError ('Text "IntLog n 0 is infinite") :: TInt | |
| IntLog _1 ('Pos 0) = TypeError ('Text "IntLog n 0 is infinite") :: TInt | |
| IntLog _1 ('Neg 0) = TypeError ('Text "IntLog n 0 is infinite") :: TInt | |
| IntLog _1 (0 ':% _2 :: Rat) = TypeError ('Text "IntLog n 0 is infinite") :: TInt | |
| IntLog _1 (_2 ':% 0 :: Rat) = TypeError ('Text "IntLog parameter has zero denominator") :: TInt | |
| IntLog _1 ('Neg _2 :: TInt) = TypeError ('Text "IntLog of a negative does not exist") :: TInt | |
| IntLog n (x :: Nat) = IntLog n ('Pos x) | |
| IntLog n ('Pos x :: TInt) = IntLogAux n ('Pos x) | |
| IntLog n (x :: Rat) = If (Floor x == 'Pos 0) (NegLogFudge n x (Negate (IntLogAux n (Floor (Recip x))))) (IntLog n (Floor x)) |
type Log2 (x :: k) = IntLog 2 x Source #
The floor of the logarithm base 2 of a type-level number.
Note that unlike Log2, this errors on Log2 0.
Since: 0.1.4
Symbols
(Kind) This is the kind of type-level symbols.
Instances
| SingKind Symbol | Since: base-4.9.0.0 | ||||
Defined in GHC.Internal.Generics Associated Types
| |||||
| TestCoercion SSymbol | Since: base-4.18.0.0 | ||||
Defined in GHC.Internal.TypeLits | |||||
| TestEquality SSymbol | Since: base-4.18.0.0 | ||||
Defined in GHC.Internal.TypeLits | |||||
| KnownSymbol a => SingI (a :: Symbol) | Since: base-4.9.0.0 | ||||
Defined in GHC.Internal.Generics Methods sing :: Sing a | |||||
| type DemoteRep Symbol | |||||
Defined in GHC.Internal.Generics | |||||
| data Sing (s :: Symbol) | |||||
Defined in GHC.Internal.Generics | |||||
| type Compare (a :: Symbol) (b :: Symbol) | |||||
Defined in GHC.Internal.Data.Type.Ord | |||||
type family AppendSymbol (a :: Symbol) (b :: Symbol) :: Symbol where ... #
Concatenation of type-level symbols.
Since: base-4.10.0.0
type family CmpSymbol (a :: Symbol) (b :: Symbol) :: Ordering where ... #
Comparison of type-level symbols, as a function.
Since: base-4.7.0.0
class KnownSymbol (n :: Symbol) #
This class gives the string associated with a type-level symbol. There are instances of the class for every concrete literal: "hello", etc.
Since: base-4.7.0.0
Minimal complete definition
symbolVal :: forall (n :: Symbol) proxy. KnownSymbol n => proxy n -> String #
Since: base-4.7.0.0
symbolVal' :: forall (n :: Symbol). KnownSymbol n => Proxy# n -> String #
Since: base-4.8.0.0
data SomeSymbol #
This type represents unknown type-level symbols.
Constructors
| KnownSymbol n => SomeSymbol (Proxy n) | Since: base-4.7.0.0 |
Instances
| Read SomeSymbol | Since: base-4.7.0.0 |
Defined in GHC.Internal.TypeLits Methods readsPrec :: Int -> ReadS SomeSymbol # readList :: ReadS [SomeSymbol] # readPrec :: ReadPrec SomeSymbol # readListPrec :: ReadPrec [SomeSymbol] # | |
| Show SomeSymbol | Since: base-4.7.0.0 |
Defined in GHC.Internal.TypeLits Methods showsPrec :: Int -> SomeSymbol -> ShowS # show :: SomeSymbol -> String # showList :: [SomeSymbol] -> ShowS # | |
| Eq SomeSymbol | Since: base-4.7.0.0 |
Defined in GHC.Internal.TypeLits | |
| Ord SomeSymbol | Since: base-4.7.0.0 |
Defined in GHC.Internal.TypeLits Methods compare :: SomeSymbol -> SomeSymbol -> Ordering # (<) :: SomeSymbol -> SomeSymbol -> Bool # (<=) :: SomeSymbol -> SomeSymbol -> Bool # (>) :: SomeSymbol -> SomeSymbol -> Bool # (>=) :: SomeSymbol -> SomeSymbol -> Bool # max :: SomeSymbol -> SomeSymbol -> SomeSymbol # min :: SomeSymbol -> SomeSymbol -> SomeSymbol # | |
someSymbolVal :: String -> SomeSymbol #
Convert a string into an unknown type-level symbol.
Since: base-4.7.0.0
sameSymbol :: forall (a :: Symbol) (b :: Symbol) proxy1 proxy2. (KnownSymbol a, KnownSymbol b) => proxy1 a -> proxy2 b -> Maybe (a :~: b) #
We either get evidence that this function was instantiated with the
same type-level symbols, or Nothing.
Since: base-4.7.0.0
User-defined type errors
type family TypeError (a :: ErrorMessage) :: b where ... #
The type-level equivalent of error.
The polymorphic kind of this type allows it to be used in several settings. For instance, it can be used as a constraint, e.g. to provide a better error message for a non-existent instance,
-- in a context
instance TypeError (Text "Cannot Show functions." :$$:
Text "Perhaps there is a missing argument?")
=> Show (a -> b) where
showsPrec = error "unreachable"
It can also be placed on the right-hand side of a type-level function to provide an error for an invalid case,
type family ByteSize x where
ByteSize Word16 = 2
ByteSize Word8 = 1
ByteSize a = TypeError (Text "The type " :<>: ShowType a :<>:
Text " is not exportable.")
Since: base-4.9.0.0
data ErrorMessage #
A description of a custom type error.
Constructors
| Text Symbol | Show the text as is. |
| ShowType t | Pretty print the type.
|
| ErrorMessage :<>: ErrorMessage infixl 6 | Put two pieces of error message next to each other. |
| ErrorMessage :$$: ErrorMessage infixl 5 | Stack two pieces of error message on top of each other. |