Portability | portable |
---|---|
Stability | experimental |
Maintainer | libraries@haskell.org |
Data.Typeable
Contents
Description
The Typeable
class reifies types to some extent by associating type
representations to types. These type representations can be compared,
and one can in turn define a type-safe cast operation. To this end,
an unsafe cast is guarded by a test for type (representation)
equivalence. The module Data.Dynamic uses Typeable for an
implementation of dynamics. The module Data.Data uses Typeable
and type-safe cast (but not dynamics) to support the "Scrap your
boilerplate" style of generic programming.
- class Typeable a where
- cast :: (Typeable a, Typeable b) => a -> Maybe b
- gcast :: (Typeable a, Typeable b) => c a -> Maybe (c b)
- data TypeRep
- data TyCon
- showsTypeRep :: TypeRep -> ShowS
- mkTyCon :: String -> TyCon
- mkTyConApp :: TyCon -> [TypeRep] -> TypeRep
- mkAppTy :: TypeRep -> TypeRep -> TypeRep
- mkFunTy :: TypeRep -> TypeRep -> TypeRep
- splitTyConApp :: TypeRep -> (TyCon, [TypeRep])
- funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
- typeRepTyCon :: TypeRep -> TyCon
- typeRepArgs :: TypeRep -> [TypeRep]
- tyConString :: TyCon -> String
- typeRepKey :: TypeRep -> IO Int
- class Typeable1 t where
- class Typeable2 t where
- class Typeable3 t where
- class Typeable4 t where
- class Typeable5 t where
- class Typeable6 t where
- class Typeable7 t where
- gcast1 :: (Typeable1 t, Typeable1 t') => c (t a) -> Maybe (c (t' a))
- gcast2 :: (Typeable2 t, Typeable2 t') => c (t a b) -> Maybe (c (t' a b))
- typeOfDefault :: forall t a. (Typeable1 t, Typeable a) => t a -> TypeRep
- typeOf1Default :: forall t a b. (Typeable2 t, Typeable a) => t a b -> TypeRep
- typeOf2Default :: forall t a b c. (Typeable3 t, Typeable a) => t a b c -> TypeRep
- typeOf3Default :: forall t a b c d. (Typeable4 t, Typeable a) => t a b c d -> TypeRep
- typeOf4Default :: forall t a b c d e. (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
- typeOf5Default :: forall t a b c d e f. (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
- typeOf6Default :: forall t a b c d e f g. (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
The Typeable class
class Typeable a where
The class Typeable
allows a concrete representation of a type to
be calculated.
Methods
Instances
Type-safe cast
gcast :: (Typeable a, Typeable b) => c a -> Maybe (c b)
A flexible variation parameterised in a type constructor
Type representations
data TypeRep
A concrete representation of a (monomorphic) type. TypeRep
supports reasonably efficient equality.
data TyCon
showsTypeRep :: TypeRep -> ShowS
Construction of type representations
Arguments
:: String | the name of the type constructor (should be unique in the program, so it might be wise to use the fully qualified name). |
-> TyCon | A unique |
Builds a TyCon
object representing a type constructor. An
implementation of Data.Typeable should ensure that the following holds:
mkTyCon "a" == mkTyCon "a"
mkTyConApp :: TyCon -> [TypeRep] -> TypeRep
Applies a type constructor to a sequence of types
mkFunTy :: TypeRep -> TypeRep -> TypeRep
A special case of mkTyConApp
, which applies the function
type constructor to a pair of types.
Observation of type representations
splitTyConApp :: TypeRep -> (TyCon, [TypeRep])
Splits a type constructor application
funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
typeRepTyCon :: TypeRep -> TyCon
Observe the type constructor of a type representation
typeRepArgs :: TypeRep -> [TypeRep]
Observe the argument types of a type representation
tyConString :: TyCon -> String
Observe string encoding of a type representation
typeRepKey :: TypeRep -> IO Int
Returns a unique integer associated with a TypeRep
. This can
be used for making a mapping with TypeReps
as the keys, for example. It is guaranteed that t1 == t2
if and only if
typeRepKey t1 == typeRepKey t2
.
It is in the IO
monad because the actual value of the key may
vary from run to run of the program. You should only rely on
the equality property, not any actual key value. The relative ordering
of keys has no meaning either.
The other Typeable classes
Note: The general instances are provided for GHC only.
class Typeable1 t where
Variant for unary type constructors
Instances
Typeable1 [] | |
Typeable1 Ratio | |
Typeable1 StablePtr | |
Typeable1 IO | |
Typeable1 Ptr | |
Typeable1 FunPtr | |
Typeable1 Maybe | |
Typeable1 MVar | |
Typeable1 IORef | |
Typeable1 ForeignPtr | |
Typeable1 Weak | |
Typeable1 TVar | |
Typeable1 STM | |
Typeable1 Chan | |
Typeable1 Complex | |
Typeable1 Fixed | |
Typeable1 StableName | |
(Typeable2 s, Typeable a) => Typeable1 (s a) | One Typeable1 instance for all Typeable2 instances |
class Typeable2 t where
Variant for binary type constructors
class Typeable3 t where
Variant for 3-ary type constructors
class Typeable4 t where
Variant for 4-ary type constructors
class Typeable5 t where
Variant for 5-ary type constructors
class Typeable6 t where
Variant for 6-ary type constructors
class Typeable7 t where
Variant for 7-ary type constructors
Default instances
Note: These are not needed by GHC, for which these instances are generated by general instance declarations.
typeOfDefault :: forall t a. (Typeable1 t, Typeable a) => t a -> TypeRep
typeOf1Default :: forall t a b. (Typeable2 t, Typeable a) => t a b -> TypeRep
typeOf2Default :: forall t a b c. (Typeable3 t, Typeable a) => t a b c -> TypeRep
typeOf3Default :: forall t a b c d. (Typeable4 t, Typeable a) => t a b c d -> TypeRep
typeOf4Default :: forall t a b c d e. (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
typeOf5Default :: forall t a b c d e f. (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep