jinquantities-0.1.0: Unit conversion and manipulation library.

Safe HaskellSafe
LanguageHaskell2010

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"

data SimpleUnit Source #

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

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

Constructors

SimpleUnit 

Fields

data CompoundUnit Source #

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

Constructors

CompoundUnit 

Fields

  • defs :: Definitions

    Definitions used to create the units.

  • sUnits :: [SimpleUnit]

    List of SimpleUnits that is interpreted as the units being multiplied together.

showCompUnit' :: SimpleUnit -> String Source #

Show a single unit, but prepend with / if negative

showPower :: Double -> String Source #

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 

Fields

  • magnitude :: a

    Numerical magnitude of quantity.

    >>> magnitude <$> fromString "100 N * m"
    Right 100.0
    
  • units :: CompoundUnit

    Units associated with quantity.

    >>> units <$> fromString "3.4 m/s^2"
    Right meter / second ** 2
    
Instances
Eq a => Eq (Quantity a) Source # 
Instance details

Defined in Data.Quantities.Data

Methods

(==) :: Quantity a -> Quantity a -> Bool #

(/=) :: Quantity a -> Quantity a -> Bool #

Ord a => Ord (Quantity a) Source # 
Instance details

Defined in Data.Quantities.Data

Methods

compare :: 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 details

Defined in Data.Quantities.Data

Methods

showsPrec :: 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.

defs' :: Quantity a -> Definitions Source #

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 details

Defined in Data.Quantities.Data

Show a => Show (QuantityError a) Source # 
Instance details

Defined in Data.Quantities.Data

type QuantityComputation a = Either (QuantityError a) Source #

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
  x <- fromString "BADUNIT"
  convertBase x

Returns Left (UndefinedUnitError BADUNIT)

reduceUnits :: Quantity a -> Quantity a Source #

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.

invertSimpleUnit :: SimpleUnit -> SimpleUnit Source #

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 Definition Source #

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

data Definitions Source #

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

Constructors

Definitions 

Fields

emptyDefinitions :: Definitions Source #

Default, empty set of definitions.

unionDefinitions :: Definitions -> Definitions -> Definitions Source #

Combine two Definitions structures