jinquantities-0.1.1: Unit conversion and manipulation library.

Data.Quantities.Data

Description

Base module for all data structures.

Synopsis

# Documentation

>>> import Control.Applicative
>>> import Data.Quantities


type Symbol = String Source #

String representation of a unit. Examples: "meter", "foot"

Representation of single unit. For example: "mm^2" is represented as

SimpleUnit { symbol = "meter", prefix = "milli", power = 2.0 }

Constructors

 SimpleUnit Fieldssymbol :: String prefix :: String power :: Double
Instances
 Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data MethodsshowList :: [SimpleUnit] -> ShowS #

Data type to hold compound units, which are simple units multiplied together.

Constructors

 CompoundUnit Fieldsdefs :: DefinitionsDefinitions used to create the units.sUnits :: [SimpleUnit]List of SimpleUnits that is interpreted as the units being multiplied together.
Instances
 Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data MethodsshowList :: [CompoundUnit] -> ShowS #

Show a single unit, but prepend with / if negative

Removes decimal if almost integer.

showPrettyNum :: (Show a, Num a) => a -> String Source #

Will be used when we allow pretty printing of fractional units.

data Quantity a Source #

Combination of magnitude and units.

Constructors

 Quantity Fieldsmagnitude :: aNumerical magnitude of quantity.>>> magnitude <$> fromString "100 N * m" Right 100.0 units :: CompoundUnitUnits associated with quantity.>>> units <$> fromString "3.4 m/s^2" Right meter / second ** 2 
Instances
 Eq a => Eq (Quantity a) Source # Instance detailsDefined in Data.Quantities.Data Methods(==) :: Quantity a -> Quantity a -> Bool #(/=) :: Quantity a -> Quantity a -> Bool # Ord a => Ord (Quantity a) Source # Instance detailsDefined in Data.Quantities.Data Methodscompare :: Quantity a -> Quantity a -> Ordering #(<) :: Quantity a -> Quantity a -> Bool #(<=) :: Quantity a -> Quantity a -> Bool #(>) :: Quantity a -> Quantity a -> Bool #(>=) :: Quantity a -> Quantity a -> Bool #max :: Quantity a -> Quantity a -> Quantity a #min :: Quantity a -> Quantity a -> Quantity a # Show a => Show (Quantity a) Source # Instance detailsDefined in Data.Quantities.Data MethodsshowsPrec :: Int -> Quantity a -> ShowS #show :: Quantity a -> String #showList :: [Quantity a] -> ShowS #

units' :: Quantity a -> [SimpleUnit] Source #

Convenience function to extract SimpleUnit collection from Quantity's CompoundUnit.

Convenience function to extract Definitions from Quantity's CompoundUnit.

baseQuant :: a -> [SimpleUnit] -> Quantity a Source #

Convenience function to make quantity with no definitions.

showSort :: [SimpleUnit] -> [SimpleUnit] Source #

Sort units but put negative units at end.

data QuantityError a Source #

Custom error type

Constructors

 UndefinedUnitError String Used when trying to parse an undefined unit. DimensionalityError CompoundUnit CompoundUnit Used when converting units that do not have the same dimensionality (example: convert meter to second). UnitAlreadyDefinedError String Used internally when defining units and a unit is already defined. PrefixAlreadyDefinedError String Used internally when defining units and a prefix is already defined. ParserError String Used when a string cannot be parsed. DifferentDefinitionsError CompoundUnit CompoundUnit Used when two quantities come from different Definitions. ScalingFactorError (Quantity a) Used when a scaling factor is present in a unit conversion.
Instances
 Eq a => Eq (QuantityError a) Source # Instance detailsDefined in Data.Quantities.Data Methods(==) :: QuantityError a -> QuantityError a -> Bool #(/=) :: QuantityError a -> QuantityError a -> Bool # Show a => Show (QuantityError a) Source # Instance detailsDefined in Data.Quantities.Data MethodsshowsPrec :: Int -> QuantityError a -> ShowS #show :: QuantityError a -> String #showList :: [QuantityError a] -> ShowS #

Useful for monadic computations with QuantityErrors. Some examples:

computation :: QuantityComputation Quantity
computation = do
x <- fromString "mile/hr"
y <- unitsFromString "m/s"
convert x y

Returns Right 0.44704 meter / second

computation :: QuantityComputation Quantity
computation = do
convertBase x

Returns Left (UndefinedUnitError BADUNIT)

Combines equivalent units and removes units with powers of zero.

reduceUnits' :: [SimpleUnit] -> [SimpleUnit] Source #

Helper function for reduceUnits.

removeZeros :: [SimpleUnit] -> [SimpleUnit] Source #

Removes units with powers of zero that are left over from other computations.

invertUnits :: [SimpleUnit] -> [SimpleUnit] Source #

Negate the powers of a list of SimpleUnits.

Inverts unit by negating the power field.

multiplyQuants :: Num a => Quantity a -> Quantity a -> Quantity a Source #

Multiplies two quantities.

divideQuants :: Fractional a => Quantity a -> Quantity a -> Quantity a Source #

Divides two quantities.

exptQuants :: (Real a, Floating a) => Quantity a -> a -> Quantity a Source #

Exponentiates a quantity with an integer

Data type for the three definition types. Used to hold definitions information when parsing.

Constructors

 PrefixDefinition FieldsdefPrefix :: Symbol factor :: Double defSynonyms :: [Symbol] BaseDefinition Fieldsbase :: Symbol dimBase :: Symbol defSynonyms :: [Symbol] UnitDefinition FieldsdefSymbol :: Symbol quantity :: Quantity Double defSynonyms :: [Symbol]
Instances
 Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data MethodsshowList :: [Definition] -> ShowS #

Holds information about defined units, prefixes, and bases. Used when parsing new units and performing units conversions.

Constructors

 Definitions Fieldsbases :: Map String (Double, [SimpleUnit])Map from symbol to base units and conversion factor to those units.synonyms :: Map String StringMap from alias to symbol. Symbols without aliases are present as identity maps.unitsList :: [String]List of all units (no aliases). Used in prefix parser, and to detect duplicate definitions.prefixes :: [String]List of all prefixes (no aliases). Used in prefix parser, and to detect duplicate prefix definitions.prefixValues :: Map String DoubleMultiplicative factor of prefixes.prefixSynonyms :: Map String StringMap from prefix alias to prefix.unitTypes :: Map String StringMap from base symbols to unit types.defStringHash :: IntHash of the definitions string used to create definitions. Defaults to -1 if modified or no string was used.
Instances
 Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data Methods Source # Instance detailsDefined in Data.Quantities.Data MethodsshowList :: [Definitions] -> ShowS #

Default, empty set of definitions.

Combine two Definitions structures