typenums-0.1.3: Type level numbers using existing Nat functionality
Copyright(c) 2018-2021 Iris Ward
LicenseBSD3
Maintaineraditu.venyhandottir@gmail.com
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell2010

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 level numbers

Naturals

data Nat #

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

Instances

Instances details
KnownNat n => KnownInt (n :: Nat) Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

intSing :: SInt n

class KnownNat (n :: Nat) #

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

natSing

natVal :: forall (n :: Nat) proxy. KnownNat n => proxy n -> Integer #

Since: base-4.7.0.0

natVal' :: forall (n :: Nat). KnownNat n => Proxy# n -> Integer #

Since: base-4.8.0.0

data SomeNat #

This type represents unknown type-level natural numbers.

Since: base-4.10.0.0

Constructors

KnownNat n => SomeNat (Proxy n) 

Instances

Instances details
Eq SomeNat

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeNats

Methods

(==) :: SomeNat -> SomeNat -> Bool #

(/=) :: SomeNat -> SomeNat -> Bool #

Ord SomeNat

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeNats

Read SomeNat

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeNats

Show SomeNat

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeNats

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). (KnownNat a, KnownNat b) => Proxy a -> Proxy 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

data TInt Source #

(Kind) An integer that may be negative.

Constructors

Pos Nat 
Neg Nat 

Instances

Instances details
KnownNat n => KnownInt ('Pos n :: TInt) Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

intSing :: SInt ('Pos n)

KnownNat n => KnownInt ('Neg n :: TInt) Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

intSing :: SInt ('Neg n)

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

Instances

Instances details
KnownNat n => KnownInt (n :: Nat) Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

intSing :: SInt n

KnownNat n => KnownInt ('Pos n :: TInt) Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

intSing :: SInt ('Pos n)

KnownNat n => KnownInt ('Neg n :: TInt) Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

intSing :: SInt ('Neg n)

intVal :: forall n proxy. KnownInt n => proxy n -> Integer Source #

Get the value associated with a type-level integer

intVal' :: forall n. KnownInt n => Proxy# n -> Integer Source #

Get the value associated with a type-level integer. The difference between this function and intVal is that it takes a Proxy# parameter, which has zero runtime representation and so is entirely free.

data SomeInt Source #

This type represents unknown type-level integers.

Since: 0.1.1

Constructors

forall n.KnownInt n => SomeInt (Proxy n) 

Instances

Instances details
Eq SomeInt Source # 
Instance details

Defined in Data.TypeNums.Ints

Methods

(==) :: SomeInt -> SomeInt -> Bool #

(/=) :: SomeInt -> SomeInt -> Bool #

Ord SomeInt Source # 
Instance details

Defined in Data.TypeNums.Ints

Read SomeInt Source # 
Instance details

Defined in Data.TypeNums.Ints

Show SomeInt Source # 
Instance details

Defined in Data.TypeNums.Ints

someIntVal :: Integer -> SomeInt Source #

Convert an integer into an unknown type-level integer.

Since: 0.1.1

Rationals

data Rat Source #

Type constructor for a rational

Constructors

forall k. k :% Nat 

Instances

Instances details
(KnownInt n, KnownNat d, d /= 0) => KnownRat (n :% d :: Rat) Source # 
Instance details

Defined in Data.TypeNums.Rats

Methods

ratSing :: SRat (n :% d)

(TypeError ('Text "Denominator must not equal 0") :: Constraint) => KnownRat (n :% 0 :: Rat) Source # 
Instance details

Defined in Data.TypeNums.Rats

Methods

ratSing :: SRat (n :% 0)

class KnownRat r 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

Instances details
KnownInt n => KnownRat (n :: k) Source # 
Instance details

Defined in Data.TypeNums.Rats

Methods

ratSing :: SRat n

(KnownInt n, KnownNat d, d /= 0) => KnownRat (n :% d :: Rat) Source # 
Instance details

Defined in Data.TypeNums.Rats

Methods

ratSing :: SRat (n :% d)

(TypeError ('Text "Denominator must not equal 0") :: Constraint) => KnownRat (n :% 0 :: Rat) Source # 
Instance details

Defined in Data.TypeNums.Rats

Methods

ratSing :: SRat (n :% 0)

ratVal :: forall proxy r. KnownRat r => proxy r -> Rational Source #

Get the value associated with a type-level rational

ratVal' :: forall r. KnownRat r => Proxy# r -> Rational Source #

Get the value associated with a type-level rational. The difference between this function and ratVal is that it takes a Proxy# parameter, which has zero runtime representation and so is entirely free.

data SomeRat Source #

This type represents unknown type-level integers.

Since: 0.1.1

Constructors

forall r.KnownRat r => SomeRat (Proxy r) 

Instances

Instances details
Eq SomeRat Source # 
Instance details

Defined in Data.TypeNums.Rats

Methods

(==) :: SomeRat -> SomeRat -> Bool #

(/=) :: SomeRat -> SomeRat -> Bool #

Ord SomeRat Source # 
Instance details

Defined in Data.TypeNums.Rats

Read SomeRat Source # 
Instance details

Defined in Data.TypeNums.Rats

Show SomeRat Source # 
Instance details

Defined in Data.TypeNums.Rats

someRatVal :: Rational -> SomeRat Source #

Convert a rational into an unknown type-level rational.

Since: 0.1.1

Type level numerical operations

Comparison

type (==?) (a :: k) (b :: k) = (==) a b infix 4 Source #

Boolean type-level equals. Useful for e.g. If (x ==? 0)

type (/=?) (a :: k) (b :: k) = Not ((==) a b) infix 4 Source #

Boolean type-level not-equals.

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) <=? ('Pos b) = (<=?) a b 
('Neg _) <=? ('Pos _) = 'True 
('Pos 0) <=? ('Neg 0) = 'True 
('Pos _) <=? ('Neg _) = 'False 
('Pos a) <=? b = a <=? b 
a <=? ('Pos b) = a <=? b 
0 <=? ('Neg 0) = 'True 
(a :: Nat) <=? ('Neg _) = 'False 
('Neg a) <=? (b :: Nat) = 'True 
('Neg a) <=? ('Neg b) = (<=?) b a 
(n :% 0) <=? _ = TypeError ('Text "Denominator must not equal 0") 
_ <=? (n :% 0) = TypeError ('Text "Denominator must not equal 0") 
(n1 :% d1) <=? (n2 :% d2) = (n1 * d2) <=? (n2 * d1) 
a <=? (n :% d) = (a * d) <=? n 
(n :% d) <=? b = n <=? (b * d) 

type (==) (a :: k) (b :: k) = (==) a b ~ 'True infix 4 Source #

Equality constraint, used as e.g. (x == 3) => _

type (/=) (a :: k) (b :: k) = (==) a b ~ 'False infix 4 Source #

Not-equal constraint

type (<=) (a :: k1) (b :: k2) = (a <=? b) ~ 'True infix 4 Source #

type (<) (a :: k1) (b :: k2) = (b <=? a) ~ 'False infix 4 Source #

type (>=) (a :: k1) (b :: k2) = (b <=? a) ~ 'True infix 4 Source #

type (>) (a :: k1) (b :: k2) = (a <=? b) ~ 'False infix 4 Source #

Arithmetic

type (+) a b = Add a b infixl 6 Source #

The sum of two type-level numbers

type (-) a b = Sub a b infixl 6 Source #

The difference of two type-level numbers

type * a b = 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 family (a :: Nat) ^ (b :: Nat) :: Nat where ... infixr 8 #

Exponentiation of type-level naturals.

Since: base-4.7.0.0

Symbols

data Symbol #

(Kind) This is the kind of type-level symbols. Declared here because class IP needs it

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

symbolSing

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

Instances details
Eq SomeSymbol

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeLits

Ord SomeSymbol

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeLits

Read SomeSymbol

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeLits

Show SomeSymbol

Since: base-4.7.0.0

Instance details

Defined in GHC.TypeLits

someSymbolVal :: String -> SomeSymbol #

Convert a string into an unknown type-level symbol.

Since: base-4.7.0.0

sameSymbol :: forall (a :: Symbol) (b :: Symbol). (KnownSymbol a, KnownSymbol b) => Proxy a -> Proxy 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. ShowType :: k -> ErrorMessage

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.