base-4.8.1.0: Basic libraries

Copyright | (c) The University of Glasgow, CWI 2001--2004
License | BSD-style (see the file libraries/base/LICENSE)
Maintainer | libraries@haskell.org
Stability | experimental
Portability | portable
Safe Haskell | Trustworthy
Language | Haskell2010

Data.Typeable

Contents

- The Typeable class
- Propositional equality
- For backwards compatibility
- Type-safe cast
- Generalized casts for higher-order kinds
- A canonical proxy type
- Type representations
- Construction of type representations
- Observation of type representations

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.

## Compatibility Notes

Since GHC 7.8, `Typeable` is poly-kinded. The changes required for this might break some old programs involving `Typeable`. More details on this, including how to fix your code, can be found on the PolyTypeable wiki page

Synopsis

- class Typeable a
- typeRep :: forall proxy a. Typeable a => proxy a -> TypeRep
- data a :~: b where
  - Refl :: a :~: a
- typeOf :: forall a. Typeable a => a -> TypeRep
- typeOf1 :: forall t a. Typeable t => t a -> TypeRep
- typeOf2 :: forall t a b. Typeable t => t a b -> TypeRep
- typeOf3 :: forall t a b c. Typeable t => t a b c -> TypeRep
- typeOf4 :: forall t a b c d. Typeable t => t a b c d -> TypeRep
- typeOf5 :: forall t a b c d e. Typeable t => t a b c d e -> TypeRep
- typeOf6 :: forall t a b c d e f. Typeable t => t a b c d e f -> TypeRep
- typeOf7 :: forall t a b c d e f g. Typeable t => t a b c d e f g -> TypeRep
- type Typeable1 a = Typeable a
- type Typeable2 a = Typeable a
- type Typeable3 a = Typeable a
- type Typeable4 a = Typeable a
- type Typeable5 a = Typeable a
- type Typeable6 a = Typeable a
- type Typeable7 a = Typeable a
- cast :: forall a b. (Typeable a, Typeable b) => a -> Maybe b
- eqT :: forall a b. (Typeable a, Typeable b) => Maybe (a :~: b)
- gcast :: forall a b c. (Typeable a, Typeable b) => c a -> Maybe (c b)
- gcast1 :: forall c t t' a. (Typeable t, Typeable t') => c (t a) -> Maybe (c (t' a))
- gcast2 :: forall c t t' a b. (Typeable t, Typeable t') => c (t a b) -> Maybe (c (t' a b))
- data Proxy t = Proxy
- data TypeRep
- typeRepFingerprint :: TypeRep -> Fingerprint
- rnfTypeRep :: TypeRep -> ()
- showsTypeRep :: TypeRep -> ShowS
- data TyCon
- tyConFingerprint :: TyCon -> Fingerprint
- tyConString :: TyCon -> String
- tyConPackage :: TyCon -> String
- tyConModule :: TyCon -> String
- tyConName :: TyCon -> String
- rnfTyCon :: TyCon -> ()
- mkTyCon3 :: String -> String -> 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]

# The Typeable class

class Typeable a Source

The class `Typeable` allows a concrete representation of a type to be calculated.

Minimal complete definition

typeRep#

typeRep :: forall proxy a. Typeable a => proxy a -> TypeRep Source

Takes a value of type `a` and returns a concrete representation of that type.

Since: 4.7.0.0

# Propositional equality

data a :~: b where infix 4 Source

Propositional equality. If `a :~: b` is inhabited by some terminating value, then the type `a` is the same as the type `b`. To use this equality in practice, pattern-match on the `a :~: b` to get out the `Refl` constructor; in the body of the pattern-match, the compiler knows that `a ~ b`.

Since: 4.7.0.0

Constructors

Refl :: a :~: a |

Instances

Category k ((:~:) k) |
TestEquality k ((:~:) k a) |
TestCoercion k ((:~:) k a) |
(~) k a b => Bounded ((:~:) k a b) |
(~) k a b => Enum ((:~:) k a b) |
Eq ((:~:) k a b) |
((~) * a b, Data a) => Data ((:~:) * a b) |
Ord ((:~:) k a b) |
(~) k a b => Read ((:~:) k a b) |
Show ((:~:) k a b) |

# For backwards compatibility

typeOf :: forall a. Typeable a => a -> TypeRep Source

typeOf1 :: forall t a. Typeable t => t a -> TypeRep Source

typeOf2 :: forall t a b. Typeable t => t a b -> TypeRep Source

typeOf3 :: forall t a b c. Typeable t => t a b c -> TypeRep Source

typeOf4 :: forall t a b c d. Typeable t => t a b c d -> TypeRep Source

typeOf5 :: forall t a b c d e. Typeable t => t a b c d e -> TypeRep Source

typeOf6 :: forall t a b c d e f. Typeable t => t a b c d e f -> TypeRep Source

typeOf7 :: forall t a b c d e f g. Typeable t => t a b c d e f g -> TypeRep Source

type Typeable1 a = Typeable a Source

Deprecated: renamed to `Typeable`

type Typeable2 a = Typeable a Source

Deprecated: renamed to `Typeable`

type Typeable3 a = Typeable a Source

Deprecated: renamed to `Typeable`

type Typeable4 a = Typeable a Source

Deprecated: renamed to `Typeable`

type Typeable5 a = Typeable a Source

Deprecated: renamed to `Typeable`

type Typeable6 a = Typeable a Source

Deprecated: renamed to `Typeable`

type Typeable7 a = Typeable a Source

Deprecated: renamed to `Typeable`

# Type-safe cast

cast :: forall a b. (Typeable a, Typeable b) => a -> Maybe b Source

The type-safe cast operation

eqT :: forall a b. (Typeable a, Typeable b) => Maybe (a :~: b) Source

Extract a witness of equality of two types

Since: 4.7.0.0

gcast :: forall a b c. (Typeable a, Typeable b) => c a -> Maybe (c b) Source

A flexible variation parameterised in a type constructor

# Generalized casts for higher-order kinds

gcast1 :: forall c t t' a. (Typeable t, Typeable t') => c (t a) -> Maybe (c (t' a)) Source

Cast over `k1 -> k2`

gcast2 :: forall c t t' a b. (Typeable t, Typeable t') => c (t a b) -> Maybe (c (t' a b)) Source

Cast over `k1 -> k2 -> k3`

# A canonical proxy type

data Proxy t Source

A concrete, poly-kinded proxy type

Constructors

Proxy |

Instances

Monad (Proxy *) |
Functor (Proxy *) |
Applicative (Proxy *) |
Foldable (Proxy *) |
Traversable (Proxy *) |
Bounded (Proxy k s) |
Enum (Proxy k s) |
Eq (Proxy k s) |
Data t => Data (Proxy * t) |
Ord (Proxy k s) |
Read (Proxy k s) |
Show (Proxy k s) |
Ix (Proxy k s) |
Generic (Proxy * t) |
Monoid (Proxy k s) |
type Rep (Proxy k t) |

# Type representations

data TypeRep Source

A concrete representation of a (monomorphic) type. `TypeRep` supports reasonably efficient equality.

Instances

Eq TypeRep |
Ord TypeRep |
Show TypeRep |

typeRepFingerprint :: TypeRep -> Fingerprint Source

Observe the `Fingerprint` of a type representation

Since: 4.8.0.0

rnfTypeRep :: TypeRep -> () Source

Helper to fully evaluate `TypeRep` for use as `NFData(rnf)` implementation

Since: 4.8.0.0

showsTypeRep :: TypeRep -> ShowS Source

data TyCon Source

An abstract representation of a type constructor. `TyCon` objects can be built using `mkTyCon`.

Instances

Eq TyCon |
Ord TyCon |
Show TyCon |

tyConFingerprint :: TyCon -> Fingerprint Source

Since: 4.8.0.0

tyConString :: TyCon -> String Source

Deprecated: renamed to `tyConName`; `tyConModule` and `tyConPackage` are also available.

Observe string encoding of a type representation

tyConPackage :: TyCon -> String Source

Since: 4.5.0.0

tyConModule :: TyCon -> String Source

Since: 4.5.0.0

tyConName :: TyCon -> String Source

Since: 4.5.0.0

rnfTyCon :: TyCon -> () Source

Helper to fully evaluate `TyCon` for use as `NFData(rnf)` implementation

Since: 4.8.0.0

# Construction of type representations

mkTyCon3 Source

Arguments

:: String | package name
-> String | module name
-> String | the name of the type constructor
-> TyCon | A unique `TyCon` object

Builds a `TyCon` object representing a type constructor. An implementation of Data.Typeable should ensure that the following holds:

     A==A' ^ B==B' ^ C==C' ==> mkTyCon A B C == mkTyCon A' B' C'

mkTyConApp :: TyCon -> [TypeRep] -> TypeRep Source

Applies a monomorphic type constructor to a sequence of types

mkAppTy :: TypeRep -> TypeRep -> TypeRep Source

Adds a TypeRep argument to a TypeRep.

mkFunTy :: TypeRep -> TypeRep -> TypeRep Source

A special case of `mkTyConApp`, which applies the function type constructor to a pair of types.

# Observation of type representations

splitTyConApp :: TypeRep -> (TyCon, [TypeRep]) Source

Splits a type constructor application. Note that if the type construcotr is polymorphic, this will not return the kinds that were used. See `splitPolyTyConApp` if you need all parts.

funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep Source

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 Source

Observe the type constructor of a type representation

typeRepArgs :: TypeRep -> [TypeRep] Source

Observe the argument types of a type representation