dimensional-1.5: Statically checked physical dimensions

Copyright | Copyright (C) 2006-2018 Bjorn Buckwalter
License | BSD3
Maintainer | bjorn@buckwalter.se
Stability | Experimental
Portability | GHC only?
Safe Haskell | Safe-Inferred
Language | Haskell2010

Numeric.Units.Dimensional.Coercion

Description

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 `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.

Synopsis

- coerce :: forall {k :: RuntimeRep} (a :: TYPE k) (b :: TYPE k). Coercible a b => a -> b
- data family Dimensional v :: Dimension -> Type -> Type
- unQuantity :: SQuantity s d a -> a

# Documentation

coerce :: forall {k :: RuntimeRep} (a :: TYPE k) (b :: TYPE k). Coercible a b => a -> b Source #

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

Expand

    >>> 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]

data family Dimensional v :: Dimension -> Type -> Type Source #

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

#### Instances

Instances details

Generic1 (Dimensional ('DQuantity s) d :: Type -> Type) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Associated Types type Rep1 (Dimensional ('DQuantity s) d) :: k -> Type Source # Methods from1 :: forall (a :: k). Dimensional ('DQuantity s) d a -> Rep1 (Dimensional ('DQuantity s) d) a Source # to1 :: forall (a :: k). Rep1 (Dimensional ('DQuantity s) d) a -> Dimensional ('DQuantity s) d a Source #
Generic1 (Dimensional ('DUnit m) d :: Type -> Type) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Associated Types type Rep1 (Dimensional ('DUnit m) d) :: k -> Type Source # Methods from1 :: forall (a :: k). Dimensional ('DUnit m) d a -> Rep1 (Dimensional ('DUnit m) d) a Source # to1 :: forall (a :: k). Rep1 (Dimensional ('DUnit m) d) a -> Dimensional ('DUnit m) d a Source #
Vector Vector a => Vector Vector (SQuantity s d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

Methods

basicUnsafeFreeze :: Mutable Vector s0 (SQuantity s d a) -> ST s0 (Vector (SQuantity s d a)) Source #

basicUnsafeThaw :: Vector (SQuantity s d a) -> ST s0 (Mutable Vector s0 (SQuantity s d a)) Source #

basicLength :: Vector (SQuantity s d a) -> Int Source #

basicUnsafeSlice :: Int -> Int -> Vector (SQuantity s d a) -> Vector (SQuantity s d a) Source #

basicUnsafeIndexM :: Vector (SQuantity s d a) -> Int -> Box (SQuantity s d a) Source #

basicUnsafeCopy :: Mutable Vector s0 (SQuantity s d a) -> Vector (SQuantity s d a) -> ST s0 () Source #

elemseq :: Vector (SQuantity s d a) -> SQuantity s d a -> b -> b Source #

MVector MVector a => MVector MVector (SQuantity s d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

Methods

basicLength :: MVector s0 (SQuantity s d a) -> Int Source #

basicUnsafeSlice :: Int -> Int -> MVector s0 (SQuantity s d a) -> MVector s0 (SQuantity s d a) Source #

basicOverlaps :: MVector s0 (SQuantity s d a) -> MVector s0 (SQuantity s d a) -> Bool Source #

basicUnsafeNew :: Int -> ST s0 (MVector s0 (SQuantity s d a)) Source #

basicInitialize :: MVector s0 (SQuantity s d a) -> ST s0 () Source #

basicUnsafeReplicate :: Int -> SQuantity s d a -> ST s0 (MVector s0 (SQuantity s d a)) Source #

basicUnsafeRead :: MVector s0 (SQuantity s d a) -> Int -> ST s0 (SQuantity s d a) Source #

basicUnsafeWrite :: MVector s0 (SQuantity s d a) -> Int -> SQuantity s d a -> ST s0 () Source #

basicClear :: MVector s0 (SQuantity s d a) -> ST s0 () Source #

basicSet :: MVector s0 (SQuantity s d a) -> SQuantity s d a -> ST s0 () Source #

basicUnsafeCopy :: MVector s0 (SQuantity s d a) -> MVector s0 (SQuantity s d a) -> ST s0 () Source #

basicUnsafeMove :: MVector s0 (SQuantity s d a) -> MVector s0 (SQuantity s d a) -> ST s0 () Source #

basicUnsafeGrow :: MVector s0 (SQuantity s d a) -> Int -> ST s0 (MVector s0 (SQuantity s d a)) Source #

KnownDimension d => Demotable (Quantity d) Source # |
Instance details Defined in Numeric.Units.Dimensional.Dynamic Methods demotableOut :: Quantity d a -> AnyQuantity a
Eq1 (SQuantity s d) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods liftEq :: (a -> b -> Bool) -> SQuantity s d a -> SQuantity s d b -> Bool Source #
Ord1 (SQuantity s d) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods liftCompare :: (a -> b -> Ordering) -> SQuantity s d a -> SQuantity s d b -> Ordering Source #
KnownVariant v => Functor (Dimensional v d) Source # | A `Functor` instance for `Dimensional`. Note that this instance is dubious, because it allows you to break the dimensional abstraction. See `dmap` for more information. Note that, while this instance overlaps with that given for `Dimensionless`, it is confluent with that instance. Note that this is an orphan instance.
Instance details Defined in Numeric.Units.Dimensional.Functor Methods fmap :: (a -> b) -> Dimensional v d a -> Dimensional v d b Source # (<$) :: a -> Dimensional v d b -> Dimensional v d a Source #
Functor (SQuantity s DOne) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods fmap :: (a -> b) -> SQuantity s DOne a -> SQuantity s DOne b Source # (<$) :: a -> SQuantity s DOne b -> SQuantity s DOne a Source #
NFData a => NFData (Quantity d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods rnf :: Quantity d a -> () Source #
(Typeable s, Typeable d, Data a) => Data (Dimensional ('DQuantity s) d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

Methods

gfoldl :: (forall d0 b. Data d0 => c (d0 -> b) -> d0 -> c b) -> (forall g. g -> c g) -> Dimensional ('DQuantity s) d a -> c (Dimensional ('DQuantity s) d a) Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Dimensional ('DQuantity s) d a) Source #

toConstr :: Dimensional ('DQuantity s) d a -> Constr Source #

dataTypeOf :: Dimensional ('DQuantity s) d a -> DataType Source #

dataCast1 :: Typeable t => (forall d0. Data d0 => c (t d0)) -> Maybe (c (Dimensional ('DQuantity s) d a)) Source #

dataCast2 :: Typeable t => (forall d0 e. (Data d0, Data e) => c (t d0 e)) -> Maybe (c (Dimensional ('DQuantity s) d a)) Source #

gmapT :: (forall b. Data b => b -> b) -> Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d0. Data d0 => d0 -> r') -> Dimensional ('DQuantity s) d a -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d0. Data d0 => d0 -> r') -> Dimensional ('DQuantity s) d a -> r Source #

gmapQ :: (forall d0. Data d0 => d0 -> u) -> Dimensional ('DQuantity s) d a -> [u] Source #

gmapQi :: Int -> (forall d0. Data d0 => d0 -> u) -> Dimensional ('DQuantity s) d a -> u Source #

gmapM :: Monad m => (forall d0. Data d0 => d0 -> m d0) -> Dimensional ('DQuantity s) d a -> m (Dimensional ('DQuantity s) d a) Source #

gmapMp :: MonadPlus m => (forall d0. Data d0 => d0 -> m d0) -> Dimensional ('DQuantity s) d a -> m (Dimensional ('DQuantity s) d a) Source #

gmapMo :: MonadPlus m => (forall d0. Data d0 => d0 -> m d0) -> Dimensional ('DQuantity s) d a -> m (Dimensional ('DQuantity s) d a) Source #

Storable a => Storable (SQuantity s d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

Methods

sizeOf :: SQuantity s d a -> Int Source #

alignment :: SQuantity s d a -> Int Source #

peekElemOff :: Ptr (SQuantity s d a) -> Int -> IO (SQuantity s d a) Source #

pokeElemOff :: Ptr (SQuantity s d a) -> Int -> SQuantity s d a -> IO () Source #

peekByteOff :: Ptr b -> Int -> IO (SQuantity s d a) Source #

pokeByteOff :: Ptr b -> Int -> SQuantity s d a -> IO () Source #

peek :: Ptr (SQuantity s d a) -> IO (SQuantity s d a) Source #

poke :: Ptr (SQuantity s d a) -> SQuantity s d a -> IO () Source #

Num a => Monoid (SQuantity s d a) Source # | `Quantity`s of a given `Dimension` form a `Monoid` under addition.
Instance details Defined in Numeric.Units.Dimensional.Internal Methods mempty :: SQuantity s d a Source # mappend :: SQuantity s d a -> SQuantity s d a -> SQuantity s d a Source # mconcat :: [SQuantity s d a] -> SQuantity s d a Source #
Num a => Semigroup (SQuantity s d a) Source # | `Quantity`s of a given `Dimension` form a `Semigroup` under addition.
Instance details Defined in Numeric.Units.Dimensional.Internal Methods (<>) :: SQuantity s d a -> SQuantity s d a -> SQuantity s d a Source # sconcat :: NonEmpty (SQuantity s d a) -> SQuantity s d a Source # stimes :: Integral b => b -> SQuantity s d a -> SQuantity s d a Source #
Bounded a => Bounded (SQuantity s d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods minBound :: SQuantity s d a Source # maxBound :: SQuantity s d a Source #
Generic (Dimensional ('DQuantity s) d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Associated Types type Rep (Dimensional ('DQuantity s) d a) :: Type -> Type Source # Methods from :: Dimensional ('DQuantity s) d a -> Rep (Dimensional ('DQuantity s) d a) x Source # to :: Rep (Dimensional ('DQuantity s) d a) x -> Dimensional ('DQuantity s) d a Source #
Generic (Dimensional ('DUnit m) d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Associated Types type Rep (Dimensional ('DUnit m) d a) :: Type -> Type Source # Methods from :: Dimensional ('DUnit m) d a -> Rep (Dimensional ('DUnit m) d a) x Source # to :: Rep (Dimensional ('DUnit m) d a) x -> Dimensional ('DUnit m) d a Source #
(KnownDimension d, KnownExactPi s, Show a, Real a) => Show (SQuantity s d a) Source # | Uses non-breaking spaces between the value and the unit, and within the unit name.
Instance details Defined in Numeric.Units.Dimensional.Internal Methods showsPrec :: Int -> SQuantity s d a -> ShowS Source # show :: SQuantity s d a -> String Source # showList :: [SQuantity s d a] -> ShowS Source #
Show a => Show (Unit m d a) Source # | Unit names are shown with non-breaking spaces.
Instance details Defined in Numeric.Units.Dimensional.Internal Methods showsPrec :: Int -> Unit m d a -> ShowS Source # show :: Unit m d a -> String Source # showList :: [Unit m d a] -> ShowS Source #
KnownDimension d => HasDimension (Dimensional v d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods dimension :: Dimensional v d a -> Dimension' Source #
KnownDimension d => HasDynamicDimension (Dimensional v d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods dynamicDimension :: Dimensional v d a -> DynamicDimension Source #
HasInterchangeName (Unit m d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods interchangeName :: Unit m d a -> InterchangeName Source #
Eq a => Eq (Dimensional ('DQuantity s) d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods (==) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source # (/=) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source #
Ord a => Ord (Dimensional ('DQuantity s) d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

Methods

compare :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Ordering Source #

(<) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source #

(<=) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source #

(>) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source #

(>=) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source #

max :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a Source #

min :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a Source #

AEq a => AEq (Dimensional ('DQuantity s) d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal Methods (===) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source # (~==) :: Dimensional ('DQuantity s) d a -> Dimensional ('DQuantity s) d a -> Bool Source #
Unbox a => Unbox (SQuantity s d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal
type Rep1 (Dimensional ('DQuantity s) d :: Type -> Type) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal type Rep1 (Dimensional ('DQuantity s) d :: Type -> Type) = D1 ('MetaData "Dimensional" "Numeric.Units.Dimensional.Internal" "dimensional-1.5-Jh8KVMZ2g1X80m3wFNP7DL" 'True) (C1 ('MetaCons "Quantity" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
type Rep1 (Dimensional ('DUnit m) d :: Type -> Type) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

type Rep1 (Dimensional ('DUnit m) d :: Type -> Type) = D1 ('MetaData "Dimensional" "Numeric.Units.Dimensional.Internal" "dimensional-1.5-Jh8KVMZ2g1X80m3wFNP7DL" 'False) (C1 ('MetaCons "Unit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (UnitName m)) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ExactPi) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)))

newtype MVector v (SQuantity s d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

newtype MVector v (SQuantity s d a) = MV_Quantity {

- unMVQ :: MVector v a

}

newtype Dimensional ('DQuantity s) d a Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal newtype Dimensional ('DQuantity s) d a = Quantity a
data Dimensional ('DUnit m) d a Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal data Dimensional ('DUnit m) d a = Unit !(UnitName m) !ExactPi !a
type Rep (Dimensional ('DQuantity s) d a) Source # |
Instance details Defined in Numeric.Units.Dimensional.Internal type Rep (Dimensional ('DQuantity s) d a) = D1 ('MetaData "Dimensional" "Numeric.Units.Dimensional.Internal" "dimensional-1.5-Jh8KVMZ2g1X80m3wFNP7DL" 'True) (C1 ('MetaCons "Quantity" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))
type Rep (Dimensional ('DUnit m) d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

type Rep (Dimensional ('DUnit m) d a) = D1 ('MetaData "Dimensional" "Numeric.Units.Dimensional.Internal" "dimensional-1.5-Jh8KVMZ2g1X80m3wFNP7DL" 'False) (C1 ('MetaCons "Unit" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (UnitName m)) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ExactPi) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a))))

newtype Vector (SQuantity s d a) Source # |

Instance details

Defined in Numeric.Units.Dimensional.Internal

newtype Vector (SQuantity s d a) = V_Quantity {

- unVQ :: Vector a

}

unQuantity :: SQuantity s d a -> a Source #

Unwraps a possibly-scaled `SQuantity`, yielding its underlying representation.

This is a type-restricted version of `coerce`.