| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Units.Simple
Synopsis
- data Quantity (us :: Units) a
- fromQuantity :: Quantity us a -> a
- data Unit
- type Units = ([Unit'], [Unit'])
- type SingleUnit (u :: Unit) = '('['(u, 1)], '[])
- type UnitRepr (us :: Units) = UnitRepr' (Eval (Fst us)) (Eval (Snd us))
- showUnits :: forall us. KnownSymbol (UnitRepr us) => String
- (.+) :: (SameUnits u1 u2, Num a) => Quantity u1 a -> Quantity u2 a -> Quantity u1 a
- (.-) :: (SameUnits u1 u2, Num a) => Quantity u1 a -> Quantity u2 a -> Quantity u1 a
- (.*) :: Num a => Quantity us1 a -> Quantity us2 a -> Quantity (MergeUnits us1 us2) a
- (./) :: Fractional a => Quantity us1 a -> Quantity us2 a -> Quantity (MergeUnits us1 (Recip us2)) a
- adim :: Num a => Quantity Adimensional a
- adim' :: Num a => a -> Quantity Adimensional a
- meter :: Num a => Quantity (SingleUnit Meter) a
- meter' :: Num a => a -> Quantity (SingleUnit Meter) a
- kilogram :: Num a => Quantity (SingleUnit Kilogram) a
- kilogram' :: Num a => a -> Quantity (SingleUnit Kilogram) a
- second :: Num a => Quantity (SingleUnit Second) a
- second' :: Num a => a -> Quantity (SingleUnit Second) a
- ampere :: Num a => Quantity (SingleUnit Ampere) a
- ampere' :: Num a => a -> Quantity (SingleUnit Ampere) a
- kelvin :: Num a => Quantity (SingleUnit Kelvin) a
- kelvin' :: Num a => a -> Quantity (SingleUnit Kelvin) a
- mole :: Num a => Quantity (SingleUnit Mole) a
- mole' :: Num a => a -> Quantity (SingleUnit Mole) a
- candela :: Num a => Quantity (SingleUnit Candela) a
- candela' :: Num a => a -> Quantity (SingleUnit Candela) a
The Quantity type
data Quantity (us :: Units) a Source #
A numerical quantity with some associated unit. Units are not created directly with this constructor, but through the individual unit functions.
Examples:
>>>meter -- a unitary quantity associated to meters.1 m>>>2.5*ampere -- constructing through literal arithmetic2.5 A>>>candela' 3.14 -- constructing through the unit constructors3.14 cd>>>:set -XDataKinds>>>2 :: Quantity (SingleUnit 'Second) Rational2 % 1 s
Associated units are represented through a phantom parameter of the Units kind synonym.
These are currently implemented as a type-level pair of lists representing the power
to which each unit is raised. Units can be inspected through showUnits.
New constructors may be written by combining the provided ones, such as
>>>let newton = kilogram .* meter ./ (second .* second)>>>23*newton23.0 kg*m/s^2>>>let g = 6.67408e-11 * newton .* (meter .* meter) ./ (kilogram .* kilogram)>>>g -- gravitational constant6.67408e-11 m^3/kg*s^2>>>let gravity m1 m2 r = g .* (m1 * kilogram) .* (m2 * kilogram) ./ (r*meter .* r*meter)>>>let earth_mass = 5.972e24 * kilogram>>>let mars_mass = 6.417e23 * kilogram>>>let earth_radius = 6371e3 * meter>>>let mars_radius = 3389.5e3 * meter>>>let weight_on_earth mass = gravity mass earth_mass earth_radius>>>let weight_on_mars mass = gravity mass mars_mass mars_radius>>>weight_on_earth (80 * kilogram)785.5719790179963 kg*m/s^2>>>weight_on_mars (80 * kilogram)298.22370259533704 kg*m/s^2>>>fromQuantity $ weight_on_mars 1 / (fromQuantity $ weight_on_earth 1)0.3796261966575378
Instances
fromQuantity :: Quantity us a -> a Source #
Unwraps a Quantity, losing all unit information
type SingleUnit (u :: Unit) = '('['(u, 1)], '[]) Source #
showUnits :: forall us. KnownSymbol (UnitRepr us) => String Source #
A string representation of Units. Useful for debugging.
Basic arithmetic with quantities
(.+) :: (SameUnits u1 u2, Num a) => Quantity u1 a -> Quantity u2 a -> Quantity u1 a infixl 5 Source #
Sums two quantities with the same units. Summing quantities with different units results in a type error.
Examples:
>>>2*meter .+ 3*meter5 m>>>2*meter .+ 1*second<interactive>:16:1-19: error: • Unit mismatch: m and s • In the expression: 2 * meter .+ 1 * second In an equation for ‘it’: it = 2 * meter .+ 1 * second
(.-) :: (SameUnits u1 u2, Num a) => Quantity u1 a -> Quantity u2 a -> Quantity u1 a infixl 5 Source #
Subtracts two quantities with the same units. Subtracting quantities with different units results in a type error.
Examples:
>>>let newton = kilogram .* meter ./ (second .* second)>>>10*newton - 2*newton8.0 kg*m/s^2
(.*) :: Num a => Quantity us1 a -> Quantity us2 a -> Quantity (MergeUnits us1 us2) a infixl 6 Source #
Multiplies two quantities correctly merging their units.
Examples:
>>>meter .* meter1 m^2>>>let mps = meter ./ second>>>20*mps .* 60*second1200.0 m
Important: Though Quantity a has a Num instance for convenience, it
must not be used for anything other than interacting with literals, otherwise
the units will not be correct:
>>>2*meter * 3*meter -- note that (*) was used in place of (.*)6 m>>>2*meter .* 3*meter -- this is the correct usage6 m^2
(./) :: Fractional a => Quantity us1 a -> Quantity us2 a -> Quantity (MergeUnits us1 (Recip us2)) a infixl 6 Source #
Divides a quantity by another correctly merging their units.
Examples:
>>>let coulomb = second .* ampere>>>20*coulomb ./ 2*second10.0 A
Base SI Units
Smart constructors for quantities in all the base SI Units. The constructors are provided in both unitary (non-ticked) and function (ticked) forms.
Examples:
>>>273.0*kelvin -- unitary form273.0 K>>>kelvin' 273.0 -- function form273.0 K
adim :: Num a => Quantity Adimensional a Source #
A quantity with no associated dimension. Can be multiplied or divided by any other quantity, but can only be added to or subtracted from other adimensional quantities.
>>>2*adim + 4*adim6 <adimensional>>>>adim .+ meter<interactive>:79:1-13: error: • Unit mismatch: <adimensional> and m • In the expression: adim .+ meter In an equation for ‘it’: it = adim .+ meter