GHC.TypeLits

base-4.6.0.1: Basic libraries

Safe Haskell | None

GHC.TypeLits

Contents

- Kinds
- Linking type and value level
- Working with singletons
- Functions on type nats
- Destructing type-nats.

Description

This module is an internal GHC module. It declares the constants used in the implementation of type-level natural numbers. The programmer interface for workin with type-level naturals should be defined in a separate library.

Synopsis

- data Nat
- data Symbol
- data family Sing n
- class SingI a where
  - sing :: Sing a
- class kparam ~ Kind => SingE kparam rep | kparam -> rep where
  - fromSing :: Sing (a :: k) -> rep
- class (SingI a, SingE (Kind :: k) rep) => SingRep a rep | a -> rep
- unsafeSingNat :: Integer -> Sing (n :: Nat)
- unsafeSingSymbol :: String -> Sing (n :: Symbol)
- type Kind = Any
- withSing :: SingI a => (Sing a -> b) -> b
- singThat :: SingRep a rep => (rep -> Bool) -> Maybe (Sing a)
- class m (<=) n
- type family m (<=?) n :: Bool
- type family m (+) n :: Nat
- type family m (*) n :: Nat
- type family m (^) n :: Nat
- isZero :: Sing n -> IsZero n
- data IsZero where
  - IsZero :: IsZero 0
  - IsSucc :: !(Sing n) -> IsZero (n + 1)
- isEven :: Sing n -> IsEven n
- data IsEven where
  - IsEvenZero :: IsEven 0
  - IsEven :: !(Sing (n + 1)) -> IsEven ((2 * n) + 2)
  - IsOdd :: !(Sing n) -> IsEven ((2 * n) + 1)

# Kinds

data Nat

This is the *kind* of type-level natural numbers.

Instances

SingE Nat (Kind Nat) Integer |

data Symbol

This is the *kind* of type-level symbols.

Instances

SingE Symbol (Kind Symbol) String |

# Linking type and value level

data family Sing n

class SingI a where

The class `SingI` provides a "smart" constructor for singleton types. There are built-in instances for the singleton types corresponding to type literals.

Methods

sing :: Sing a

The only value of type `Sing a`

class kparam ~ Kind => SingE kparam rep | kparam -> rep where

A class that converts singletons of a given kind into values of some representation type (i.e., we forget the extra information carried by the singletons, and convert them to ordinary values).

Note that `fromSing` is overloaded based on the kind of the values and not their type---all types of a given kind are processed by the same instances.

Methods

fromSing :: Sing (a :: k) -> rep

Instances

SingE Nat (Kind Nat) Integer |
SingE Symbol (Kind Symbol) String |

class (SingI a, SingE (Kind :: k) rep) => SingRep a rep | a -> rep

A convenience class, useful when we need to both introduce and eliminate a given singleton value. Users should never need to define instances of this classes.

Instances

(SingI k a, SingE k (Kind k) rep) => SingRep k a rep |

unsafeSingNat :: Integer -> Sing (n :: Nat)

unsafeSingSymbol :: String -> Sing (n :: Symbol)

type Kind = Any

A type synonym useful for passing kinds as parameters.

# Working with singletons

withSing :: SingI a => (Sing a -> b) -> b

A convenience function useful when we need to name a singleton value multiple times. Without this function, each use of `sing` could potentially refer to a different singleton, and one has to use type signatures to ensure that they are the same.

singThat :: SingRep a rep => (rep -> Bool) -> Maybe (Sing a)

A convenience function that names a singleton satisfying a certain property. If the singleton does not satisfy the property, then the function returns `Nothing`. The property is expressed in terms of the underlying representation of the singleton.

# Functions on type nats

class m (<=) n

Comparsion of type-level naturals.

type family m (<=?) n :: Bool

type family m (+) n :: Nat

Addition of type-level naturals.

type family m (*) n :: Nat

Multiplication of type-level naturals.

type family m (^) n :: Nat

Exponentiation of type-level naturals.

# Destructing type-nats.

isZero :: Sing n -> IsZero n

data IsZero where

Constructors

IsZero :: IsZero 0 |
IsSucc :: !(Sing n) -> IsZero (n + 1) |

Instances

Show (IsZero n) |

isEven :: Sing n -> IsEven n

data IsEven where

Constructors

IsEvenZero :: IsEven 0 |
IsEven :: !(Sing (n + 1)) -> IsEven ((2 * n) + 2) |
IsOdd :: !(Sing n) -> IsEven ((2 * n) + 1) |

Instances

Show (IsEven n) |