This module provides functionalities for parsing and displaying SI units (and some other daily-use units). This module has 4 groups of functionalities that is for
Unit valuesUnitQuantity = (Float, Unit).Type that represents unit before prefixed, like "meter" in "millimeter".
Type that represents unit
Represents all recoverable errors that may occur during unit parsing, conversion, or arithmetic operations.
Creates a UnitSymbol, which represents an unprefixed unit
such as "meter" in "millimeter".
This function is mainly intended for defining custom unit symbols
that can later be added to ValidatedUnitSymbols for parsing.
Creates Unit value from UnitSymbol with no prefix added.
u pascal
|> toString defaultDisplayOption
--> "Pa"Creates Unit value from Prefix and UnitSymbol.
pu kilo pascal
|> toString defaultDisplayOption
--> "kPa"Creates Unit value from List ( Prefix, UnitSymbol, Int ),
where the Int value represents exponent.
The order in the list is preserved and used as the displayed order.
In complex case where you want to use / or parened exponentiation
like kg (m / s)^2, other functions may be more suited.
fromList [(centi, meter, 1), (noPrefix, second, -1)]
|> toString defaultDisplayOption
--> "cm s^-1"Represents multiplication of units.
u meter |> times (inverse (u second))
|> toString defaultDisplayOption
--> "m s^-1"Represents division with slash (not with negative exponent).
Intended to be used with |> operator.
Note that |> is left associative.
u joule |> per (u kelvin) |> times (u gram)
|> toString defaultDisplayOption
--> "(J / K) g"
u joule |> per ((u kelvin) |> times (u gram))
|> toString defaultDisplayOption
--> "J / (K g)"
u joule |> per (u kelvin) |> per (u gram)
|> toString defaultDisplayOption
--> "(J / K) / g"
u joule |> times (inverse (u kelvin)) |> per (u gram)
|> toString defaultDisplayOption
--> "J K^-1 / g"Represents exponentiation of units.
Intended to be used with |> operator.
Note that |> is left associative.
u mole |> per (pu deci meter |> toThe 3) |> toThe 2
|> toString defaultDisplayOption
--> "(mol / dm^3)^2"Syntax suger for toThe 2.
square (u meter)
|> toString defaultDisplayOption
--> "m^2"Syntax suger for toThe 3.
cubic (pu deci meter)
|> toString defaultDisplayOption
--> "dm^3"Syntax suger for toThe -1.
Note that this is different from power at display.
u mole |> times (inverse (u liter)) |> per (u second)
|> toString defaultDisplayOption
--> "mol L^-1 / s"Simplifies unit expressions by applying semantic normalization, such as:
(s^-1)^2 is converted into s^-2.m / 1, m 1 or m^1 are all converted into m. litterInSI : Unit
litterInSI = cubic (pu deci meter)
square litterInSI
|> toString defaultDisplayOption
--> "(dm^3)^2"
square litterInSI
|> simplifyUnitExpression
|> toString defaultDisplayOption
--> "dm^6"Unit parsing with this module proceeds in following pathway.
ValidatedUnitSymbols type,
whose creation may fail with name-conflicting units.ValidatedUnitSymbols into parser by toParser function.ValidatedUnitSymbols and creating parser is relatively heavy operation.parse function.You can:
defaultUnitSymbols) provided by this module,createUnitSymbol and add them to the default set using addUnitSymbols,addUnitSymbols over a list, starting from emptyUnitSymbols.Some units (e.g. minute, inch, year, atm) are defined as non-prefixable units to avoid ambiguity with SI prefixes.
Represents unit symbols usables in parsing purpose, in which no name collision could occurs. This type is exposed so that users can control which unit symbols are accepted by the parser.
Represents vacant ValidatedUnitSymbols value
Represents ValidatedUnitSymbols value
with SI standarized units and some other daily use unit.
The only way to add a user-defined unit symbol for parsing purpose.
This function fails if the new unit symbol conflicts with existing symbols or their prefixed forms.
eV : UnitSymbol
eV =
createUnitSymbol
{ display = "eV"
, parserInput = ["eV"]
, baseSIExpression =
{ meterExponent = 2
, kilogramExponent = 1
, secondExponent = -2
, kelvinExponent = 0
, ampereExponent = 0
, moleExponent = 0
, candelaExponent = 0
, numericalValue =
Ok (1.602176634e-19)
}
, allowPrefix = True
}
parser : Parser
parser =
defaultUnitSymbols
|> addUnitSymbols [eV]
|> Result.map toParser
|> Result.withDefault failureParser
parsedUnit : Unit
parsedUnit =
case parse parser "keV" of
Ok unit ->
unit
Err _ ->
emptyUnit
(200, parsedUnit)
|> convertInto (u eV)
--> Ok (toFloat (round 200000), u eV)Converts a set of unit symbols into a Parser.
If you want to build a parser that accepts only SI base units (with or without prefixes), you can write:
baseUnitsParser : Parser
baseUnitsParser =
emptyUnitSymbols
|> addUnitSymbols
[ meter, gram, second, kelvin, ampere, mole, candela ]
|> Result.map toParser
|> Result.withDefault failureParserThis gives you a parser that rejects any non-SI or user-defined unit symbols.
parse baseUnitsParser "cm s^-1"
|> Result.map (toString defaultDisplayOption)
--> Ok "cm s^-1"
parse baseUnitsParser "km / h"
|> Result.map (toString defaultDisplayOption)
|> Result.withDefault "failed to parse!"
--> "failed to parse!"This parser always fails.
Intended to be used as a fallback when addUnitSymbols fails,
which indicates a configuration error rather than a runtime parsing error.
Type alias for Parser.DeadEnd from elm/parser.
Converts unit into string with specified display options.
Type that represents configurations for displaying units as strings. This includes options about:
um s^-1 (This is default value)μm s⁻¹{\\rm{{\\mu}{m}}\\,/\\,{s^3}}.one |> per (u second) should be expressed, 1 / s or / s.\\frac{<numerator>}{<denominator>} style.default values for DisplayOption
default values for DisplayOption tuned for TeX output.
Change the output format into ascii string
Change the output format into tex string
Change the output format into unicode string
Change the unit separator into arbitrary string.
Change the slash into arbitrary string.
Change the heading slash (like "/ " in "6.02×10²³ / mol") into arbitrary string.
Change the paren into arbitrary string.
Change the expression of empty unit(1) into arbitrary string.
Display like 1 / s.
Display like / s.
Display like \\frac{{\\rm{mol}}\\,/\\,{\\rm{L}}}{{\\rm{s}}}.
This enforces displayTopNumeratorOne to be applied.
Display like \\left({\\rm{mol}}\\,/\\,{\\rm{L}}\\right)\\,/\\,{\\rm{s}}
display like "K /(mol / kg)". Note that space between "/" and "(" are removed.
cancels useCustomSpacing.
Units are combined syntactically when multiplying or dividing quantities.
This means repeated multiplications or divisions may result in large unit expressions
(e.g. m * s * s).
This is by design.
If you want to keep unit expressions compact or convert them into a specific unit,
call convertInto or normalize explicitly at appropriate points in your calculation.
Checks if two arguments have the same dimension.
u liter |> hasSameDimension (cubic (u meter))
--> True
u liter |> hasSameDimension (square (u meter))
--> FalseRepresenting phisical quantity that is unit multiplied by numerical coefficient.
Converts expression of physical quantity with unit into another unit. This fails when:
The targeted unit and start unit has different dimensions.
Units like ℃, Sv or year is involved.
(3, cubic (u meter))
|> convertInto (u liter)
|> Result.map
(\(numericalValue, unit) ->
String.fromInt (round numericalValue)
++ " "
++ toString defaultDisplayOption unit
)
--> Ok "3000 L"Converts quantity into normalized unit in the form of m^a kg^b s^c K^d A^e mol^f cd^g,
exponent 1 and unit with 0 exponent being omitted.
The numerical value is also converted accordingly.
Adds two quantities. Fails if unit for either of addend contains inconvertible unit like ℃, or two addeds have different dimensions.
Subtracts one quantity from another. Fails if unit for either subtrahend or minuend contains inconvertible unit like ℃, or subtrahend and minuend have different dimensions.
Multiply one quantity by another. Fails if unit for either of two contains inconvertible unit like ℃.
Divides one quantity by another. Fails if unit for either of two contains inconvertible unit like ℃.
Applies transformations sequentially, stopping at the first error.
The following sections list predefined unit symbols and prefixes. They are primarily reference material and are not required for understanding the core API.
Some units (e.g. minute, inch, year, atm) are defined as non-prefixable units to avoid ambiguity with SI prefixes.
Unit for length denoted as m.
Unit for mass denoted as g.
Unit for time denoted as s.
Unit for absolute temperature denoted as K.
Unit for electric current denoted as A.
Unit for amount of substance denoted as mol.
Unit for luminous intensity denoted as cd.
Unit for mass denoted as ton. Equals to 10^6 kg.
Unit for time denoted as min. Equals to 60 s.
Unit for time denoted as h. Equals to 3600 s.
Unit for time denoted as day. Equals to 86400 s.
Unit for time denoted as year. The exact definition may vary depending on context.
In this package, conversion involving this unit is not supported.
Unit for force denoted as N. Equals to kg m s^-2.
Unit for pressure denoted as Pa. Equals to N m^-2.
Unit for pressure denoted as atm. Equals to 1.01325×10^5 Pa.
Unit for pressure denoted as mmHg. Equals to 1/760 atm.
Unit for pressure denoted as torr. Equals to mmHg.
Unit for pressure denoted as bar. Equals to 10^5 Pa.
Unit for work and energy denoted as J. Equals to N・m.
Unit for power denoted as W. Equals to J / s.
Unit for temperature denoted as ℃. A temperature of t ℃ is equal to (t + 273.15) K.
In this package, conversion involving this unit is not supported.
Unit for frequency denoted as Hz. Equals to s^-1
Unit for electricity denoted as C. Equals to A・s.
Unit for voltage denoted as V. Equals to J / C.
Unit for resistance denoted as Ω. Equals to V / A.
Unit for conductivity denoted as S. Equals to A / V.
Unit for capacity denoted as F. Equals to C / V.
Unit for magnetic flux denoted as Wb. Equals to J / A.
Unit for magnetic flux density denoted as T. Equals to Wb / m^2.
Unit for self or mutual inductance denoted as H. Equals to V / (A s^-1).
Unit for luminous flux denoted as Lm. Equals to cd・sr.
Unit for illuminance denoted as Lx. Equals to Lm / m^2.
Unit for radio activity denoted as Bq. Equals to 1 / s.
Unit for absorbed dose denoted as Gy. Equals to J / kg.
Unit for exposure dose denoted as Sv. The dimension equals to J / kg,
but with different weight coefficient applied to different radio beam kind.
Unit for volume denoted as L. Equals to 10^(-3) m^3.
Unit for angle denoted as °. Equals to (π/180) rad.
Unit for angle denoted as rad.
Unit for stereo angle denoted as sr.
Unit for length denoted as in.
Unit for length denoted as ft.
Unit for length denoted as yd.
Unit for mass denoted as lb.
Type representing SI prefix ("kilo" in "kilometer" etc.)