| Safe Haskell | Trustworthy |
|---|---|
| Language | Haskell2010 |
GHC.TypeLits
Description
GHC's DataKinds language extension lifts data constructors, natural
numbers, and strings to the type level. This module provides the
primitives needed for working with type-level numbers (the Nat kind),
strings (the Symbol kind), and characters (the Char kind). It also defines the TypeError type
family, a feature that makes use of type-level strings to support user
defined type errors.
For now, this module is the API for working with type-level literals.
However, please note that it is a work in progress and is subject to change.
Once the design of the DataKinds feature is more stable, this will be
considered only an internal GHC module, and the programmer interface for
working with type-level data will be defined in a separate library.
Since: base-4.6.0.0
Synopsis
- data Natural
- type Nat = Natural
- data Symbol
- class KnownNat (n :: Nat) where
- natVal :: forall n proxy. KnownNat n => proxy n -> Integer
- natVal' :: forall n. KnownNat n => Proxy# n -> Integer
- class KnownSymbol (n :: Symbol) where
- symbolSing :: SSymbol n
- symbolVal :: forall n proxy. KnownSymbol n => proxy n -> String
- symbolVal' :: forall n. KnownSymbol n => Proxy# n -> String
- class KnownChar (n :: Char) where
- charVal :: forall n proxy. KnownChar n => proxy n -> Char
- charVal' :: forall n. KnownChar n => Proxy# n -> Char
- data SomeNat = forall n.KnownNat n => SomeNat (Proxy n)
- data SomeSymbol = forall n.KnownSymbol n => SomeSymbol (Proxy n)
- data SomeChar = forall n.KnownChar n => SomeChar (Proxy n)
- someNatVal :: Integer -> Maybe SomeNat
- someSymbolVal :: String -> SomeSymbol
- someCharVal :: Char -> SomeChar
- sameNat :: forall a b proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 b -> Maybe (a :~: b)
- sameSymbol :: forall a b proxy1 proxy2. (KnownSymbol a, KnownSymbol b) => proxy1 a -> proxy2 b -> Maybe (a :~: b)
- sameChar :: forall a b proxy1 proxy2. (KnownChar a, KnownChar b) => proxy1 a -> proxy2 b -> Maybe (a :~: b)
- data OrderingI a b where
- cmpNat :: forall a b proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 b -> OrderingI a b
- cmpSymbol :: forall a b proxy1 proxy2. (KnownSymbol a, KnownSymbol b) => proxy1 a -> proxy2 b -> OrderingI a b
- cmpChar :: forall a b proxy1 proxy2. (KnownChar a, KnownChar b) => proxy1 a -> proxy2 b -> OrderingI a b
- data SNat (n :: Nat)
- data SSymbol (s :: Symbol)
- data SChar (s :: Char)
- pattern SNat :: forall n. () => KnownNat n => SNat n
- pattern SSymbol :: forall s. () => KnownSymbol s => SSymbol s
- pattern SChar :: forall c. () => KnownChar c => SChar c
- fromSNat :: SNat n -> Integer
- fromSSymbol :: SSymbol s -> String
- fromSChar :: SChar c -> Char
- withSomeSNat :: forall rep (r :: TYPE rep). Integer -> (forall n. Maybe (SNat n) -> r) -> r
- withSomeSSymbol :: forall rep (r :: TYPE rep). String -> (forall s. SSymbol s -> r) -> r
- withSomeSChar :: forall rep (r :: TYPE rep). Char -> (forall c. SChar c -> r) -> r
- withKnownNat :: forall n rep (r :: TYPE rep). SNat n -> (KnownNat n => r) -> r
- withKnownSymbol :: forall s rep (r :: TYPE rep). SSymbol s -> (KnownSymbol s => r) -> r
- withKnownChar :: forall c rep (r :: TYPE rep). SChar c -> (KnownChar c => r) -> r
- type (<=) x y = Assert (x <=? y) (LeErrMsg x y)
- type (<=?) m n = OrdCond (Compare m n) 'True 'True 'False
- type family (m :: Nat) + (n :: Nat) :: Nat
- type family (m :: Nat) * (n :: Nat) :: Nat
- type family (m :: Nat) ^ (n :: Nat) :: Nat
- type family (m :: Nat) - (n :: Nat) :: Nat
- type family Div (m :: Nat) (n :: Nat) :: Nat
- type family Mod (m :: Nat) (n :: Nat) :: Nat
- type family Log2 (m :: Nat) :: Nat
- type family AppendSymbol (m :: Symbol) (n :: Symbol) :: Symbol
- type family CmpNat (m :: Natural) (n :: Natural) :: Ordering
- type family CmpSymbol (m :: Symbol) (n :: Symbol) :: Ordering
- type family CmpChar (a :: Char) (b :: Char) :: Ordering
- type family ConsSymbol (a :: Char) (b :: Symbol) :: Symbol
- type family UnconsSymbol (a :: Symbol) :: Maybe (Char, Symbol)
- type family CharToNat (c :: Char) :: Nat
- type family NatToChar (n :: Nat) :: Char
- type family TypeError (a :: ErrorMessage) :: b where ...
- data ErrorMessage
- = Text Symbol
- | forall t. ShowType t
- | ErrorMessage :<>: ErrorMessage
- | ErrorMessage :$$: ErrorMessage
Kinds
Natural number
Invariant: numbers <= 0xffffffffffffffff use the NS constructor
Instances
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
(Kind) This is the kind of type-level symbols.
Instances
| TestCoercion SSymbol Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeLits | |
| TestEquality SSymbol Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeLits | |
| type Compare (a :: Symbol) (b :: Symbol) Source # | |
Defined in Data.Type.Ord | |
Linking type and value level
class KnownNat (n :: Nat) where Source #
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
class KnownSymbol (n :: Symbol) where Source #
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
Methods
symbolSing :: SSymbol n Source #
symbolVal :: forall n proxy. KnownSymbol n => proxy n -> String Source #
Since: base-4.7.0.0
symbolVal' :: forall n. KnownSymbol n => Proxy# n -> String Source #
Since: base-4.8.0.0
This type represents unknown type-level natural numbers.
Since: base-4.10.0.0
Instances
| Read SomeNat Source # | Since: base-4.7.0.0 |
| Show SomeNat Source # | Since: base-4.7.0.0 |
| Eq SomeNat Source # | Since: base-4.7.0.0 |
| Ord SomeNat Source # | Since: base-4.7.0.0 |
data SomeSymbol Source #
This type represents unknown type-level symbols.
Constructors
| forall n.KnownSymbol n => SomeSymbol (Proxy n) | Since: base-4.7.0.0 |
Instances
| Read SomeSymbol Source # | Since: base-4.7.0.0 |
Defined in GHC.TypeLits | |
| Show SomeSymbol Source # | Since: base-4.7.0.0 |
Defined in GHC.TypeLits | |
| Eq SomeSymbol Source # | Since: base-4.7.0.0 |
Defined in GHC.TypeLits Methods (==) :: SomeSymbol -> SomeSymbol -> Bool Source # (/=) :: SomeSymbol -> SomeSymbol -> Bool Source # | |
| Ord SomeSymbol Source # | Since: base-4.7.0.0 |
Defined in GHC.TypeLits Methods compare :: SomeSymbol -> SomeSymbol -> Ordering Source # (<) :: SomeSymbol -> SomeSymbol -> Bool Source # (<=) :: SomeSymbol -> SomeSymbol -> Bool Source # (>) :: SomeSymbol -> SomeSymbol -> Bool Source # (>=) :: SomeSymbol -> SomeSymbol -> Bool Source # max :: SomeSymbol -> SomeSymbol -> SomeSymbol Source # min :: SomeSymbol -> SomeSymbol -> SomeSymbol Source # | |
Instances
| Read SomeChar Source # | |
| Show SomeChar Source # | |
| Eq SomeChar Source # | |
| Ord SomeChar Source # | |
Defined in GHC.TypeLits | |
someNatVal :: Integer -> Maybe SomeNat Source #
Convert an integer into an unknown type-level natural.
Since: base-4.7.0.0
someSymbolVal :: String -> SomeSymbol Source #
Convert a string into an unknown type-level symbol.
Since: base-4.7.0.0
someCharVal :: Char -> SomeChar Source #
Convert a character into an unknown type-level char.
Since: base-4.16.0.0
sameNat :: forall a b proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 b -> Maybe (a :~: b) Source #
We either get evidence that this function was instantiated with the
same type-level numbers, or Nothing.
Since: base-4.7.0.0
sameSymbol :: forall a b proxy1 proxy2. (KnownSymbol a, KnownSymbol b) => proxy1 a -> proxy2 b -> Maybe (a :~: b) Source #
We either get evidence that this function was instantiated with the
same type-level symbols, or Nothing.
Since: base-4.7.0.0
sameChar :: forall a b proxy1 proxy2. (KnownChar a, KnownChar b) => proxy1 a -> proxy2 b -> Maybe (a :~: b) Source #
We either get evidence that this function was instantiated with the
same type-level characters, or Nothing.
Since: base-4.16.0.0
data OrderingI a b where Source #
Ordering data type for type literals that provides proof of their ordering.
Since: base-4.16.0.0
Constructors
| LTI :: Compare a b ~ 'LT => OrderingI a b | |
| EQI :: Compare a a ~ 'EQ => OrderingI a a | |
| GTI :: Compare a b ~ 'GT => OrderingI a b |
cmpNat :: forall a b proxy1 proxy2. (KnownNat a, KnownNat b) => proxy1 a -> proxy2 b -> OrderingI a b Source #
Like sameNat, but if the numbers aren't equal, this additionally
provides proof of LT or GT.
Since: base-4.16.0.0
cmpSymbol :: forall a b proxy1 proxy2. (KnownSymbol a, KnownSymbol b) => proxy1 a -> proxy2 b -> OrderingI a b Source #
Like sameSymbol, but if the symbols aren't equal, this additionally
provides proof of LT or GT.
Since: base-4.16.0.0
cmpChar :: forall a b proxy1 proxy2. (KnownChar a, KnownChar b) => proxy1 a -> proxy2 b -> OrderingI a b Source #
Like sameChar, but if the Chars aren't equal, this additionally
provides proof of LT or GT.
Since: base-4.16.0.0
Singleton values
A value-level witness for a type-level natural number. This is commonly
referred to as a singleton type, as for each n, there is a single value
that inhabits the type (aside from bottom).SNat n
The definition of SNat is intentionally left abstract. To obtain an SNat
value, use one of the following:
- The
natSingmethod ofKnownNat. - The
SNatpattern synonym. - The
withSomeSNatfunction, which creates anSNatfrom aNaturalnumber.
Since: base-4.18.0.0
Instances
| TestCoercion SNat Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeNats | |
| TestEquality SNat Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeNats | |
| Show (SNat n) Source # | Since: base-4.18.0.0 |
data SSymbol (s :: Symbol) Source #
A value-level witness for a type-level symbol. This is commonly referred
to as a singleton type, as for each s, there is a single value that
inhabits the type (aside from bottom).SSymbol s
The definition of SSymbol is intentionally left abstract. To obtain an
SSymbol value, use one of the following:
- The
symbolSingmethod ofKnownSymbol. - The
SSymbolpattern synonym. - The
withSomeSSymbolfunction, which creates anSSymbolfrom aString.
Since: base-4.18.0.0
Instances
| TestCoercion SSymbol Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeLits | |
| TestEquality SSymbol Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeLits | |
| Show (SSymbol s) Source # | Since: base-4.18.0.0 |
data SChar (s :: Char) Source #
A value-level witness for a type-level character. This is commonly referred
to as a singleton type, as for each c, there is a single value that
inhabits the type (aside from bottom).SChar c
The definition of SChar is intentionally left abstract. To obtain an
SChar value, use one of the following:
- The
charSingmethod ofKnownChar. - The
SCharpattern synonym. - The
withSomeSCharfunction, which creates anSCharfrom aChar.
Since: base-4.18.0.0
Instances
| TestCoercion SChar Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeLits | |
| TestEquality SChar Source # | Since: base-4.18.0.0 |
Defined in GHC.TypeLits | |
| Show (SChar c) Source # | Since: base-4.18.0.0 |
pattern SNat :: forall n. () => KnownNat n => SNat n Source #
A explicitly bidirectional pattern synonym relating an SNat to a
KnownNat constraint.
As an expression: Constructs an explicit value from an
implicit SNat n constraint:KnownNat n
SNat @n ::KnownNatn =>SNatn
As a pattern: Matches on an explicit value bringing
an implicit SNat n constraint into scope:KnownNat n
f :: SNat n -> ..
f SNat = {- SNat n in scope -}
Since: base-4.18.0.0
pattern SSymbol :: forall s. () => KnownSymbol s => SSymbol s Source #
A explicitly bidirectional pattern synonym relating an SSymbol to a
KnownSymbol constraint.
As an expression: Constructs an explicit value from an
implicit SSymbol s constraint:KnownSymbol s
SSymbol @s ::KnownSymbols =>SSymbols
As a pattern: Matches on an explicit value bringing
an implicit SSymbol s constraint into scope:KnownSymbol s
f :: SSymbol s -> ..
f SSymbol = {- SSymbol s in scope -}
Since: base-4.18.0.0
pattern SChar :: forall c. () => KnownChar c => SChar c Source #
A explicitly bidirectional pattern synonym relating an SChar to a
KnownChar constraint.
As an expression: Constructs an explicit value from an
implicit SChar c constraint:KnownChar c
SChar @c ::KnownCharc =>SCharc
As a pattern: Matches on an explicit value bringing
an implicit SChar c constraint into scope:KnownChar c
f :: SChar c -> ..
f SChar = {- SChar c in scope -}
Since: base-4.18.0.0
fromSNat :: SNat n -> Integer Source #
Return the Integer corresponding to n in an value.
The returned SNat nInteger is always non-negative.
For a version of this function that returns a Natural instead of an
Integer, see fromSNat in GHC.TypeNats.
Since: base-4.18.0.0
fromSSymbol :: SSymbol s -> String Source #
Return the String corresponding to s in an value.SSymbol s
Since: base-4.18.0.0
withSomeSNat :: forall rep (r :: TYPE rep). Integer -> (forall n. Maybe (SNat n) -> r) -> r Source #
Attempt to convert an Integer into an value, where SNat nn is a
fresh type-level natural number. If the Integer argument is non-negative,
invoke the continuation with Just sn, where sn is the value.
If the SNat nInteger argument is negative, invoke the continuation with
Nothing.
For a version of this function where the continuation uses 'SNat n
instead of Maybe (SNat n)@, see withSomeSNat in GHC.TypeNats.
Since: base-4.18.0.0
withKnownSymbol :: forall s rep (r :: TYPE rep). SSymbol s -> (KnownSymbol s => r) -> r Source #
Convert an explicit value into an implicit SSymbol s
constraint.KnownSymbol s
Since: base-4.18.0.0
Functions on type literals
type (<=) x y = Assert (x <=? y) (LeErrMsg x y) infix 4 Source #
Comparison (<=) of comparable types, as a constraint.
Since: base-4.16.0.0
type (<=?) m n = OrdCond (Compare m n) 'True 'True 'False infix 4 Source #
Comparison (<=) of comparable types, as a function.
Since: base-4.16.0.0
type family (m :: Nat) + (n :: Nat) :: Nat infixl 6 Source #
Addition of type-level naturals.
Since: base-4.7.0.0
type family (m :: Nat) * (n :: Nat) :: Nat infixl 7 Source #
Multiplication of type-level naturals.
Since: base-4.7.0.0
type family (m :: Nat) ^ (n :: Nat) :: Nat infixr 8 Source #
Exponentiation of type-level naturals.
Since: base-4.7.0.0
type family (m :: Nat) - (n :: Nat) :: Nat infixl 6 Source #
Subtraction of type-level naturals.
Since: base-4.7.0.0
type family Div (m :: Nat) (n :: Nat) :: Nat infixl 7 Source #
Division (round down) of natural numbers.
Div x 0 is undefined (i.e., it cannot be reduced).
Since: base-4.11.0.0
type family Mod (m :: Nat) (n :: Nat) :: Nat infixl 7 Source #
Modulus of natural numbers.
Mod x 0 is undefined (i.e., it cannot be reduced).
Since: base-4.11.0.0
type family Log2 (m :: Nat) :: Nat Source #
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 AppendSymbol (m :: Symbol) (n :: Symbol) :: Symbol Source #
Concatenation of type-level symbols.
Since: base-4.10.0.0
type family CmpNat (m :: Natural) (n :: Natural) :: Ordering Source #
Comparison of type-level naturals, as a function.
Since: base-4.7.0.0
type family CmpSymbol (m :: Symbol) (n :: Symbol) :: Ordering Source #
Comparison of type-level symbols, as a function.
Since: base-4.7.0.0
type family CmpChar (a :: Char) (b :: Char) :: Ordering Source #
Comparison of type-level characters.
Since: base-4.16.0.0
type family ConsSymbol (a :: Char) (b :: Symbol) :: Symbol Source #
Extending a type-level symbol with a type-level character
Since: base-4.16.0.0
type family CharToNat (c :: Char) :: Nat Source #
Convert a character to its Unicode code point (cf. ord)
Since: base-4.16.0.0
type family NatToChar (n :: Nat) :: Char Source #
Convert a Unicode code point to a character (cf. chr)
Since: base-4.16.0.0
User-defined type errors
type family TypeError (a :: ErrorMessage) :: b where ... Source #
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 Source #
A description of a custom type error.
Constructors
| Text Symbol | Show the text as is. |
| forall t. 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. |