Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Common type functions
Synopsis
- data Nat
- data Symbol
- natValue :: forall (n :: Nat) a. (KnownNat n, Num a) => a
- natValue' :: forall (n :: Nat). KnownNat n => Word
- symbolValue :: forall (s :: Symbol). KnownSymbol s => String
- class KnownNat (n :: Nat)
- class KnownSymbol (n :: Symbol)
- type family CmpNat (a :: Nat) (b :: Nat) :: Ordering where ...
- type family CmpSymbol (a :: Symbol) (b :: Symbol) :: Ordering where ...
- type family (a :: Nat) <=? (b :: Nat) :: Bool where ...
- type (<=) (x :: Nat) (y :: Nat) = (x <=? y) ~ True
- type family (a :: Nat) + (b :: Nat) :: Nat where ...
- type family (a :: Nat) - (b :: Nat) :: Nat where ...
- type family (a :: Nat) * (b :: Nat) :: Nat where ...
- type family (a :: Nat) ^ (b :: Nat) :: Nat where ...
- type family Assert (prop :: Bool) (val :: k) (msg :: ErrorMessage) where ...
- type family If (c :: Bool) (t :: k) (e :: k) where ...
- type family Modulo (a :: Nat) (b :: Nat) where ...
- type family Same a b :: Nat where ...
- data Proxy (t :: k) :: forall k. k -> Type = Proxy
- type family TypeError (a :: ErrorMessage) :: b where ...
- data ErrorMessage where
- Text :: forall. Symbol -> ErrorMessage
- ShowType :: forall t. t -> ErrorMessage
- (:<>:) :: forall. ErrorMessage -> ErrorMessage -> ErrorMessage
- (:$$:) :: forall. ErrorMessage -> ErrorMessage -> ErrorMessage
Documentation
(Kind) This is the kind of type-level symbols. Declared here because class IP needs it
Instances
SingKind Symbol | Since: base-4.9.0.0 |
KnownSymbol a => SingI (a :: Symbol) | Since: base-4.9.0.0 |
Defined in GHC.Generics sing :: Sing a | |
data Sing (s :: Symbol) | |
Defined in GHC.Generics | |
type DemoteRep Symbol | |
Defined in GHC.Generics |
symbolValue :: forall (s :: Symbol). KnownSymbol s => String Source #
Get a Symbol value
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
natSing
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
symbolSing
type family CmpNat (a :: Nat) (b :: Nat) :: Ordering where ... #
Comparison of type-level naturals, as a function.
Since: base-4.7.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
type family (a :: Nat) <=? (b :: Nat) :: Bool where ... infix 4 #
Comparison of type-level naturals, as a function.
NOTE: The functionality for this function should be subsumed
by CmpNat
, so this might go away in the future.
Please let us know, if you encounter discrepancies between the two.
type (<=) (x :: Nat) (y :: Nat) = (x <=? y) ~ True infix 4 #
Comparison of type-level naturals, as a constraint.
Since: base-4.7.0.0
type family (a :: Nat) + (b :: Nat) :: Nat where ... infixl 6 #
Addition of type-level naturals.
Since: base-4.7.0.0
type family (a :: Nat) - (b :: Nat) :: Nat where ... infixl 6 #
Subtraction of type-level naturals.
Since: base-4.7.0.0
type family (a :: Nat) * (b :: Nat) :: Nat where ... infixl 7 #
Multiplication of type-level naturals.
Since: base-4.7.0.0
type family (a :: Nat) ^ (b :: Nat) :: Nat where ... infixr 8 #
Exponentiation of type-level naturals.
Since: base-4.7.0.0
type family Assert (prop :: Bool) (val :: k) (msg :: ErrorMessage) where ... Source #
Like: If cond t (TypeError msg)
The difference is that the TypeError doesn't appear in the RHS of the type which leads to better error messages (see GHC #14771).
For instance: type family F n where F n = If (n <=? 8) Int8 (TypeError (Text ERROR))
type family G n where G n = Assert (n <=? 8) Int8 (Text ERROR)
If GHC cannot solve `F n ~ Word`, it shows: ERROR
If GHC cannot solve `G n ~ Word`, it shows:
can't match ..
with Word
data Proxy (t :: k) :: forall k. k -> Type #
Proxy
is a type that holds no data, but has a phantom parameter of
arbitrary type (or even kind). Its use is to provide type information, even
though there is no value available of that type (or it may be too costly to
create one).
Historically,
is a safer alternative to the
Proxy
:: Proxy
a'undefined :: a'
idiom.
>>>
Proxy :: Proxy (Void, Int -> Int)
Proxy
Proxy can even hold types of higher kinds,
>>>
Proxy :: Proxy Either
Proxy
>>>
Proxy :: Proxy Functor
Proxy
>>>
Proxy :: Proxy complicatedStructure
Proxy
Instances
Generic1 (Proxy :: k -> Type) | |
Monad (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Functor (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Applicative (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Foldable (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Defined in Data.Foldable fold :: Monoid m => Proxy m -> m # foldMap :: Monoid m => (a -> m) -> Proxy a -> m # foldr :: (a -> b -> b) -> b -> Proxy a -> b # foldr' :: (a -> b -> b) -> b -> Proxy a -> b # foldl :: (b -> a -> b) -> b -> Proxy a -> b # foldl' :: (b -> a -> b) -> b -> Proxy a -> b # foldr1 :: (a -> a -> a) -> Proxy a -> a # foldl1 :: (a -> a -> a) -> Proxy a -> a # elem :: Eq a => a -> Proxy a -> Bool # maximum :: Ord a => Proxy a -> a # minimum :: Ord a => Proxy a -> a # | |
Traversable (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Alternative (Proxy :: Type -> Type) | Since: base-4.9.0.0 |
MonadPlus (Proxy :: Type -> Type) | Since: base-4.9.0.0 |
Bounded (Proxy t) | Since: base-4.7.0.0 |
Enum (Proxy s) | Since: base-4.7.0.0 |
Eq (Proxy s) | Since: base-4.7.0.0 |
Ord (Proxy s) | Since: base-4.7.0.0 |
Read (Proxy t) | Since: base-4.7.0.0 |
Show (Proxy s) | Since: base-4.7.0.0 |
Ix (Proxy s) | Since: base-4.7.0.0 |
Generic (Proxy t) | |
Semigroup (Proxy s) | Since: base-4.9.0.0 |
Monoid (Proxy s) | Since: base-4.7.0.0 |
type Rep1 (Proxy :: k -> Type) | Since: base-4.6.0.0 |
type Rep (Proxy t) | Since: base-4.6.0.0 |
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 where #
A description of a custom type error.
Text :: forall. Symbol -> ErrorMessage | Show the text as is. |
ShowType :: forall t. t -> ErrorMessage | Pretty print the type.
|
(:<>:) :: forall. ErrorMessage -> ErrorMessage -> ErrorMessage infixl 6 | Put two pieces of error message next to each other. |
(:$$:) :: forall. ErrorMessage -> ErrorMessage -> ErrorMessage infixl 5 | Stack two pieces of error message on top of each other. |