HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebase-4.20.2.0Haskell2010

GHC.TypeLits

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.

  • 13 types
  • 3 classes
  • 27 values
  • Packagebase-4.20.2.0
  • Exports62
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTypeLits.hs

Kinds

3 declarations
datadata Natural
#

Natural number

Invariant: numbers <= 0xffffffffffffffff use the NS constructor

Instances15Enum, Eq, Integral, Data, Num, Ord, …
  • Enum NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Enum
  • Eq NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Integral NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Data NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Num

    Note that Natural's Num instance isn't a ring: no element but 0 has an additive inverse. It is a semiring though.

  • Ord NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Read NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Show NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Show
  • Ix NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Ix
  • Bits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • PrintfArg NaturalDefined in base-4.20.2.0 · Text.Printf
  • TestCoercion SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • TestEquality SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • type Compare a b = CmpNat a bDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Ord
typetype Nat = Natural
#

A type synonym for Natural.

Previously, this was an opaque data type, but it was changed to a type synonym.

Instances1HasResolution
  • KnownNat n => HasResolution nDefined in base-4.20.2.0 · Data.Fixed

    For example, Fixed 1000 will give you a Fixed with a resolution of 1000.

datadata Symbol
#

(Kind) This is the kind of type-level symbols.

Instances7SingKind, TestCoercion, TestEquality, SingI, Compare, DemoteRep, …
  • SingKind SymbolDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • TestCoercion SSymbolDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • TestEquality SSymbolDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • KnownSymbol a => SingI aDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • type Compare a b = CmpSymbol a bDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Ord
  • type DemoteRep Symbol = StringDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • data SingDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics

Linking type and value level

25 declarations
classclass KnownNat (n :: Nat) where
#

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.

Methods

classclass KnownSymbol (n :: Symbol) where
#

This class gives the string associated with a type-level symbol. There are instances of the class for every concrete literal: "hello", etc.

Methods

datadata SomeNat
#

This type represents unknown type-level natural numbers.

Constructors

Instances4Eq, Ord, Read, Show
  • Eq SomeNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • Ord SomeNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • Read SomeNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • Show SomeNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
datadata SomeChar
#

Constructors

Instances4Eq, Ord, Read, Show
  • Eq SomeCharDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Ord SomeCharDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Read SomeCharDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Show SomeCharDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
valuedecideNat
  1. :: (KnownNat a, KnownNat b)
  2. => proxy1 a
  3. -> proxy2 b
  4. -> Either (a :~: b -> Void) (a :~: b)
#

We either get evidence that this function was instantiated with the same type-level numbers, or that the type-level numbers are distinct.

valuedecideChar
  1. :: (KnownChar a, KnownChar b)
  2. => proxy1 a
  3. -> proxy2 b
  4. -> Either (a :~: b -> Void) (a :~: b)
#

We either get evidence that this function was instantiated with the same type-level characters, or that the type-level characters are distinct.

Singleton values

newtypenewtype SNat (n :: Nat)
#

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 SNat n (aside from bottom).

The definition of SNat is intentionally left abstract. To obtain an SNat value, use one of the following:

  1. The natSing method of KnownNat.

  2. The SNat pattern synonym.

  3. The withSomeSNat function, which creates an SNat from a Natural number.

Instances5TestCoercion, TestEquality, Eq, Ord, Show
  • TestCoercion SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • TestEquality SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • Eq (SNat n)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • Ord (SNat n)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • Show (SNat n)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
newtypenewtype SSymbol (s :: Symbol)
#

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 SSymbol s (aside from bottom).

The definition of SSymbol is intentionally left abstract. To obtain an SSymbol value, use one of the following:

  1. The symbolSing method of KnownSymbol.

  2. The SSymbol pattern synonym.

  3. The withSomeSSymbol function, which creates an SSymbol from a String.

Instances5TestCoercion, TestEquality, Eq, Ord, Show
  • TestCoercion SSymbolDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • TestEquality SSymbolDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Eq (SSymbol s)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Ord (SSymbol s)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Show (SSymbol s)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
newtypenewtype SChar (s :: Char)
#

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 SChar c (aside from bottom).

The definition of SChar is intentionally left abstract. To obtain an SChar value, use one of the following:

  1. The charSing method of KnownChar.

  2. The SChar pattern synonym.

  3. The withSomeSChar function, which creates an SChar from a Char.

Instances5TestCoercion, TestEquality, Eq, Ord, Show
  • TestCoercion SCharDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • TestEquality SCharDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Eq (SChar c)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Ord (SChar c)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
  • Show (SChar c)Defined in ghc-internal-9.1003.0 · GHC.Internal.TypeLits
patternpattern SNat :: () => KnownNat n => SNat n
#

A explicitly bidirectional pattern synonym relating an SNat to a KnownNat constraint.

As an expression: Constructs an explicit SNat n value from an implicit KnownNat n constraint:

SNat @n :: KnownNat n => SNat n

As a pattern: Matches on an explicit SNat n value bringing an implicit KnownNat n constraint into scope:

f :: SNat n -> ..
f SNat = {- KnownNat n in scope -}
patternpattern SChar :: () => KnownChar c => SChar c
#

A explicitly bidirectional pattern synonym relating an SChar to a KnownChar constraint.

As an expression: Constructs an explicit SChar c value from an implicit KnownChar c constraint:

SChar @c :: KnownChar c => SChar c

As a pattern: Matches on an explicit SChar c value bringing an implicit KnownChar c constraint into scope:

f :: SChar c -> ..
f SChar = {- KnownChar c in scope -}
valuewithSomeSNat :: Integer -> (forall (n :: Nat). Maybe (SNat n) -> r) -> r
#

Attempt to convert an Integer into an SNat n value, where n is a fresh type-level natural number. If the Integer argument is non-negative, invoke the continuation with Just sn, where sn is the SNat n value. If the Integer 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.

Functions on type literals

17 declarations
typetype (<=) (x :: t) (y :: t) = Assert (x <=? y) (LeErrMsg x y)
#

Comparison (<=) of comparable types, as a constraint.

familytype family Div (a :: Natural) (b :: Natural) :: Natural
#

Division (round down) of natural numbers. Div x 0 is undefined (i.e., it cannot be reduced).

familytype family Mod (a :: Natural) (b :: Natural) :: Natural
#

Modulus of natural numbers. Mod x 0 is undefined (i.e., it cannot be reduced).

familytype family Log2 (a :: Natural) :: Natural
#

Log base 2 (round down) of natural numbers. Log 0 is undefined (i.e., it cannot be reduced).

User-defined type errors

2 declarations
familytype 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.")