Safe Haskell | None |
---|---|
Language | Haskell2010 |
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
- gram :: Fractional a => Quantity (SingleUnit Kilogram) a
- gram' :: Fractional 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
- yocto :: Fractional a => Quantity us a -> Quantity us a
- yocto' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- zepto :: Fractional a => Quantity us a -> Quantity us a
- zepto' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- atto :: Fractional a => Quantity us a -> Quantity us a
- atto' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- femto :: Fractional a => Quantity us a -> Quantity us a
- femto' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- pico :: Fractional a => Quantity us a -> Quantity us a
- pico' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- nano :: Fractional a => Quantity us a -> Quantity us a
- nano' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- micro :: Fractional a => Quantity us a -> Quantity us a
- micro' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- milli :: Fractional a => Quantity us a -> Quantity us a
- milli' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- centi :: Fractional a => Quantity us a -> Quantity us a
- centi' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- deci :: Fractional a => Quantity us a -> Quantity us a
- deci' :: Fractional a => (a -> Quantity us a) -> a -> Quantity us a
- deka :: Num a => Quantity us a -> Quantity us a
- deka' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- hecto :: Num a => Quantity us a -> Quantity us a
- hecto' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- kilo :: Num a => Quantity us a -> Quantity us a
- kilo' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- mega :: Num a => Quantity us a -> Quantity us a
- mega' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- giga :: Num a => Quantity us a -> Quantity us a
- giga' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- tera :: Num a => Quantity us a -> Quantity us a
- tera' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- peta :: Num a => Quantity us a -> Quantity us a
- peta' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- exa :: Num a => Quantity us a -> Quantity us a
- exa' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- zetta :: Num a => Quantity us a -> Quantity us a
- zetta' :: Num a => (a -> Quantity us a) -> a -> Quantity us a
- yotta :: Num a => Quantity us a -> Quantity us a
- yotta' :: Num a => (a -> Quantity us a) -> a -> Quantity us 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 arithmetic
2.5 A>>>
candela' 3.14 -- constructing through the unit constructors
3.14 cd>>>
:set -XDataKinds
>>>
2 :: Quantity (SingleUnit 'Second) Rational
2 % 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*newton
23.0 kg*m/s^2>>>
let g = 6.67408e-11 * newton .* (meter .* meter) ./ (kilogram .* kilogram)
>>>
g -- gravitational constant
6.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 * kilo gram
>>>
let mars_mass = 6.417e23 * kilo gram
>>>
let earth_radius = 6371 * kilo meter
>>>
let mars_radius = 3389.5 * kilo 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 * kilo gram)
785.5719790179963 kg*m/s^2>>>
weight_on_mars (80 * kilo gram)
298.22370259533704 kg*m/s^2>>>
weight_on_mars 1 / weight_on_earth 1
0.3796261966575378 <adimensional>
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 * kilo meter .+ 3 * kilo meter
5000 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*newton
8.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 .* meter
1 m^2>>>
let mps = meter ./ second
>>>
20*mps .* 60*second
1200.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 usage
6 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*second
10.0 A
Base SI Units
Smart constructors for quantities in all the base SI Units, plus grams (see "SI Prefixes"). The constructors are provided in both unitary (non-ticked) and function (ticked) forms.
Examples:
>>>
273.0*kelvin -- unitary form
273.0 K>>>
kelvin' 273.0 -- function form
273.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
another adimensional Quantity
.
>>>
2*adim + 4*adim
6 <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
gram :: Fractional a => Quantity (SingleUnit Kilogram) a Source #
A constructor for 1/1000th of a kilogram, to use with SI prefixes.
gram' :: Fractional a => a -> Quantity (SingleUnit Kilogram) a Source #
A constructor for 1/1000th of a kilogram, to use with SI prefixes.
SI Prefixes
The 20 base SI prefixes representing powers of 10. Reference: https://physics.nist.gov/cuu/Units/prefixes.html
Note: Though smart constructors for kilograms are provided, it is
recommended to use the prefixes with the gram
and gram'
constructors.