Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Defines the common constructors used to build quantities.
Synopsis
Documentation
defaultDefinitions :: Either (QuantityError Double) Definitions Source #
Default set of definitions that come predefined.
>>>
import Data.Quantities
fromString :: String -> Either (QuantityError Double) (Quantity Double) Source #
Create a Quantity by parsing a string. Uses an UndefinedUnitError
for
undefined units. Handles arithmetic expressions as well.
>>>
fromString "25 m/s"
Right 25.0 meter / second>>>
fromString "fakeunit"
Left (UndefinedUnitError "fakeunit")>>>
fromString "ft + 12in"
Right 2.0 foot
This function also supports unit conversions, by placing "=>" in between
two valid expressions. This behavior is undefined (and returns a
ScalingFactorError
) if the quantity to be converted to has a magnitude.
>>>
fromString "min => s"
Right 60.0 second>>>
fromString "2 ft + 6 in => ft"
Right 2.5 foot>>>
fromString "m => 3 ft"
Left (ScalingFactorError 3.0 foot)
Make sure not to use dimensional quantities in exponents.
>>>
fromString "m ** 2"
Right 1.0 meter ** 2>>>
fromString "m ** (2s)"
Left (ParserError "Used non-dimensionless exponent in ( Right 1.0 meter ) ** ( Right 2.0 second )")
fromString' :: Definitions -> String -> Either (QuantityError Double) (Quantity Double) Source #
Create quantities with custom definitions.
>>>
let myDefString = defaultDefString ++ "\nmy_unit = 100 s"
>>>
let (Right d) = readDefinitions myDefString
>>>
let myFromString = fromString' d
>>>
myFromString "25 my_unit"
Right 25.0 my_unit
unitsFromString :: String -> Either (QuantityError Double) CompoundUnit Source #
Parse units from a string. Equivalent to fmap units . fromString
>>>
unitsFromString "N * s"
Right newton second