Haskell Hierarchical Libraries (base package)ContentsIndex
Data.Typeable
Portability portable
Stability experimental
Maintainer libraries@haskell.org
Contents
The Typeable class
Type-safe cast
Type representations
Construction of type representations
Observation of type representations
The other Typeable classes
Default instances
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.Generics uses Typeable and type-safe cast (but not dynamics) to support the "Scrap your boilerplate" style of generic programming.
Synopsis
class Typeable a where
typeOf :: a -> TypeRep
cast :: (Typeable a, Typeable b) => a -> Maybe b
gcast :: (Typeable a, Typeable b) => c a -> Maybe (c b)
data TypeRep
data TyCon
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
typeOf1 :: t a -> TypeRep
class Typeable2 t where
typeOf2 :: t a b -> TypeRep
class Typeable3 t where
typeOf3 :: t a b c -> TypeRep
class Typeable4 t where
typeOf4 :: t a b c d -> TypeRep
class Typeable5 t where
typeOf5 :: t a b c d e -> TypeRep
class Typeable6 t where
typeOf6 :: t a b c d e f -> TypeRep
class Typeable7 t where
typeOf7 :: t a b c d e f g -> TypeRep
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 :: (Typeable1 t, Typeable a) => t a -> TypeRep
typeOf1Default :: (Typeable2 t, Typeable a) => t a b -> TypeRep
typeOf2Default :: (Typeable3 t, Typeable a) => t a b c -> TypeRep
typeOf3Default :: (Typeable4 t, Typeable a) => t a b c d -> TypeRep
typeOf4Default :: (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
typeOf5Default :: (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
typeOf6Default :: (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
typeOf :: a -> TypeRep
Takes a value of type a and returns a concrete representation of that type. The value of the argument should be ignored by any instance of Typeable, so that it is safe to pass undefined as the argument.
Instances
Typeable QSem
Typeable QSemN
Typeable ByteString
Typeable LazyByteString
Typeable Dynamic
Typeable DataType
Typeable IntSet
Typeable PackedString
(Typeable1 s, Typeable a) => Typeable (s a)
Typeable ()
Typeable Exception
Typeable IOException
Typeable ArithException
Typeable ArrayException
Typeable AsyncException
Typeable Bool
Typeable Char
Typeable Float
Typeable Double
Typeable Int
Typeable Word
Typeable Integer
Typeable Ordering
Typeable Handle
Typeable Int8
Typeable Int16
Typeable Int32
Typeable Int64
Typeable Word8
Typeable Word16
Typeable Word32
Typeable Word64
Typeable TyCon
Typeable TypeRep
Typeable RealWorld
Typeable Version
Typeable CChar
Typeable CSChar
Typeable CUChar
Typeable CShort
Typeable CUShort
Typeable CInt
Typeable CUInt
Typeable CLong
Typeable CULong
Typeable CLLong
Typeable CULLong
Typeable CFloat
Typeable CDouble
Typeable CLDouble
Typeable CPtrdiff
Typeable CSize
Typeable CWchar
Typeable CSigAtomic
Typeable CClock
Typeable CTime
Typeable CIntPtr
Typeable CUIntPtr
Typeable CIntMax
Typeable CUIntMax
Typeable WordPtr
Typeable IntPtr
Typeable ThreadId
Typeable CDev
Typeable CIno
Typeable CMode
Typeable COff
Typeable CPid
Typeable CSsize
Typeable CGid
Typeable CNlink
Typeable CUid
Typeable CCc
Typeable CSpeed
Typeable CTcflag
Typeable CRLim
Typeable Fd
Type-safe cast
cast :: (Typeable a, Typeable b) => a -> Maybe b
The type-safe cast operation
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.
Instances
Data TypeRep
Eq TypeRep
Show TypeRep
Typeable TypeRep
data TyCon
An abstract representation of a type constructor. TyCon objects can be built using mkTyCon.
Instances
Data TyCon
Eq TyCon
Show TyCon
Typeable TyCon
Construction of type representations
mkTyCon
:: Stringthe name of the type constructor (should be unique in the program, so it might be wise to use the fully qualified name).
-> TyConA unique TyCon object

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
mkAppTy :: TypeRep -> TypeRep -> TypeRep
Adds a TypeRep argument to a TypeRep.
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
Applies a type to a function type. Returns: Just u if the first argument represents a function of type t -> u and the second argument represents a function of type t. Otherwise, returns Nothing.
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 (IntMap) 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
Methods
typeOf1 :: t a -> TypeRep
Instances
Typeable1 Chan
Typeable1 Complex
Typeable1 IntMap
Typeable1 Queue
Typeable1 Seq
Typeable1 ViewL
Typeable1 ViewR
Typeable1 Set
Typeable1 Tree
(Typeable2 s, Typeable a) => Typeable1 (s a)
Typeable1 []
Typeable1 Maybe
Typeable1 Ratio
Typeable1 IO
Typeable1 MVar
Typeable1 Ptr
Typeable1 FunPtr
Typeable1 ForeignPtr
Typeable1 StablePtr
Typeable1 IORef
Typeable1 STM
Typeable1 TVar
Typeable1 Weak
Typeable1 StableName
class Typeable2 t where
Variant for binary type constructors
Methods
typeOf2 :: t a b -> TypeRep
Instances
Typeable2 UArray
Typeable2 IOArray
Typeable2 IOUArray
Typeable2 Map
(Typeable3 s, Typeable a) => Typeable2 (s a)
Typeable2 Either
Typeable2 (->)
Typeable2 Array
Typeable2 ST
Typeable2 STRef
Typeable2 (,)
class Typeable3 t where
Variant for 3-ary type constructors
Methods
typeOf3 :: t a b c -> TypeRep
Instances
Typeable3 STUArray
(Typeable4 s, Typeable a) => Typeable3 (s a)
Typeable3 STArray
Typeable3 (,,)
class Typeable4 t where
Variant for 4-ary type constructors
Methods
typeOf4 :: t a b c d -> TypeRep
Instances
(Typeable5 s, Typeable a) => Typeable4 (s a)
Typeable4 (,,,)
class Typeable5 t where
Variant for 5-ary type constructors
Methods
typeOf5 :: t a b c d e -> TypeRep
Instances
(Typeable6 s, Typeable a) => Typeable5 (s a)
Typeable5 (,,,,)
class Typeable6 t where
Variant for 6-ary type constructors
Methods
typeOf6 :: t a b c d e f -> TypeRep
Instances
(Typeable7 s, Typeable a) => Typeable6 (s a)
Typeable6 (,,,,,)
class Typeable7 t where
Variant for 7-ary type constructors
Methods
typeOf7 :: t a b c d e f g -> TypeRep
Instances
Typeable7 (,,,,,,)
gcast1 :: (Typeable1 t, Typeable1 t') => c (t a) -> Maybe (c (t' a))
Cast for * -> *
gcast2 :: (Typeable2 t, Typeable2 t') => c (t a b) -> Maybe (c (t' a b))
Cast for * -> * -> *
Default instances
Note: These are not needed by GHC, for which these instances are generated by general instance declarations.
typeOfDefault :: (Typeable1 t, Typeable a) => t a -> TypeRep
For defining a Typeable instance from any Typeable1 instance.
typeOf1Default :: (Typeable2 t, Typeable a) => t a b -> TypeRep
For defining a Typeable1 instance from any Typeable2 instance.
typeOf2Default :: (Typeable3 t, Typeable a) => t a b c -> TypeRep
For defining a Typeable2 instance from any Typeable3 instance.
typeOf3Default :: (Typeable4 t, Typeable a) => t a b c d -> TypeRep
For defining a Typeable3 instance from any Typeable4 instance.
typeOf4Default :: (Typeable5 t, Typeable a) => t a b c d e -> TypeRep
For defining a Typeable4 instance from any Typeable5 instance.
typeOf5Default :: (Typeable6 t, Typeable a) => t a b c d e f -> TypeRep
For defining a Typeable5 instance from any Typeable6 instance.
typeOf6Default :: (Typeable7 t, Typeable a) => t a b c d e f g -> TypeRep
For defining a Typeable6 instance from any Typeable7 instance.
Produced by Haddock version 0.6