HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduledimensional-1.5Haskell2010

Numeric.Units.Dimensional.Coercion

Re-exports the raw Quantity constructor from the Numeric.Units.Dimensional.Internal module, along with coerce, for convenience in converting between raw representations and dimensional values.

Note that use of these constructs requires the user to verify the dimensional safety of the conversion, because the coercion doesn't explicitly mention the unit of the representation. Note also that the Quantity constructor constructs a Numeric.Units.Dimensional.SQuantity which may have a scale factor other than One.

Note that the haddock documentation doesn't mention the Quantity constructor because it is a part of the Dimensional associated data family, but it is exported by this module.

  • 2 values
  • Packagedimensional-1.5
  • Exports4
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceCoercion.hs
valuecoerce :: Coercible a b => a -> b
#

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
Example5 expressions
newtype TTL = TTL Int deriving (Eq, Ord, Show)newtype Age = Age Int deriving (Eq, Ord, Show)coerce (Age 42) :: TTLTTL 42coerce (+ (1 :: Int)) (Age 42) :: TTLTTL 43coerce (map (+ (1 :: Int))) [Age 42, Age 24] :: [TTL][TTL 43,TTL 25]
familydata family Dimensional (v :: Variant) :: Dimension -> Type -> Type
#

A dimensional value, either a Quantity or a Unit, parameterized by its Dimension and representation.

Instances34Generic1, Demotable, Functor, Eq1, Ord1, NFData, …