coerce -package:capability

The function coerce allows you to safely convert between values of types that have the same representation with no run-time overhead. In the simplest case you can use it instead of a newtype constructor, to go from the newtype's concrete type to the abstract type. But it also works in more complicated settings, e.g. converting a list of newtypes to a list of concrete types. When used in conversions involving a newtype wrapper, make sure the newtype constructor is in scope. This function is representation-polymorphic, but the RuntimeRep type argument is marked as Inferred, meaning that it is not available for visible type application. This means the typechecker will accept coerce @Int @Age 42.

Examples

>>> newtype TTL = TTL Int deriving (Eq, Ord, Show)

>>> newtype Age = Age Int deriving (Eq, Ord, Show)

>>> coerce (Age 42) :: TTL
TTL 42

>>> coerce (+ (1 :: Int)) (Age 42) :: TTL
TTL 43

>>> coerce (map (+ (1 :: Int))) [Age 42, Age 24] :: [TTL]
[TTL 43,TTL 25]
coerce v a b coerces v : a to type b, returning a v' : b with maybe extra hidden applications or hidden abstractions. In principle, this function can host coercive subtyping, but currently it only tries to fix problems with hidden function types.
Deprecated: Use coerceInner instead
Coerce items inside a BVar.
Safely coerce between values of types that have the same representation.
If two things are equal you can convert one to the other
If two things are equal, you can convert one to the other.
Safe coercions between data types. More in-depth information can be found on the Roles wiki page
This module defines operations to coerce the type parameters of optics to a representationally equal type. For example, if we have
newtype MkInt = MkInt Int
and
l :: Lens' S Int
then
coerceA @Int @MkInt l :: Lens' S MkInt
Safe coercions between data types. More in-depth information can be found on the Roles wiki page
Types and functions used for input and result coercion.
Bidirectional version of Data.Coerce.
Sometimes GHC doesn't see that two type-level values are equal when they ought to be equal. Coerce lets us postpone the check to another day.
Type-safe cast, using representational equality
Semantically the same as coerceFromJSONKeyFunction = fmap coerce = coerce. See note on fromJSONKeyCoerce.
Data types that are representationally equal are isomorphic. This is only available on GHC 7.8+