module Data.Quantities.Data where
import Data.List (partition, sort)
import qualified Data.Map as M
type Symbol = String
data SimpleUnit = SimpleUnit { symbol :: String
, prefix :: String
, power :: Double } deriving (Eq, Ord)
instance Show SimpleUnit where
show (SimpleUnit s pr p)
| p == 1 = sym
| otherwise = sym ++ " ** " ++ showPower p
where sym = pr ++ s
data CompoundUnit = CompoundUnit { defs :: Definitions
, sUnits :: [SimpleUnit]
} deriving (Eq, Ord)
instance Show CompoundUnit where
show (CompoundUnit _ us) = unwords . map showCompUnit' $ showSort us
showCompUnit' :: SimpleUnit -> String
showCompUnit' su@(SimpleUnit _ _ p)
| p < 0 = "/ " ++ show (su { power = -p })
| otherwise = show su
{-# ANN showPower "HLint: ignore Too strict if" #-}
showPower :: Double -> String
showPower d = if isInt d then show (round d :: Integer) else show d
where isInt x = x == fromInteger (round x)
showPrettyNum :: (Show a, Num a) => a -> String
showPrettyNum x = map (pretty M.!) $ show x
where pretty = M.fromList $ zip "0123456789.-" "⁰¹²³⁴⁵⁶⁷⁸⁹·⁻"
data Quantity a = Quantity
{ magnitude :: a
, units :: CompoundUnit
} deriving (Ord)
units' :: Quantity a -> [SimpleUnit]
units' = sUnits . units
defs' :: Quantity a -> Definitions
defs' = defs . units
instance (Show a) => Show (Quantity a) where
show (Quantity m us) = show m ++ " " ++ show us
baseQuant :: a -> [SimpleUnit] -> Quantity a
baseQuant m us = Quantity m (CompoundUnit emptyDefinitions us)
showSort :: [SimpleUnit] -> [SimpleUnit]
showSort c = pos ++ neg
where (pos, neg) = partition (\q -> power q > 0) c
instance (Eq a) => Eq (Quantity a) where
(Quantity m1 u1) == (Quantity m2 u2) = m1 == m2 && sort (sUnits u1) == sort (sUnits u2)
data QuantityError a = UndefinedUnitError String
| DimensionalityError CompoundUnit CompoundUnit
| UnitAlreadyDefinedError String
| PrefixAlreadyDefinedError String
| ParserError String
| DifferentDefinitionsError CompoundUnit CompoundUnit
| ScalingFactorError (Quantity a)
deriving (Show, Eq)
type QuantityComputation a = Either (QuantityError a)
reduceUnits :: Quantity a -> Quantity a
reduceUnits q = q { units = newUnits }
where newUnits = (units q) { sUnits = reduceUnits' (units' q) }
reduceUnits' :: [SimpleUnit] -> [SimpleUnit]
reduceUnits' = removeZeros . reduceComp . sort
where reduceComp [] = []
reduceComp (SimpleUnit x pr1 p1 : SimpleUnit y pr2 p2: xs)
| (x,pr1) == (y,pr2) = SimpleUnit x pr1 (p1+p2) : reduceComp xs
| otherwise = SimpleUnit x pr1 p1 : reduceComp (SimpleUnit y pr2 p2 : xs)
reduceComp (x:xs) = x : reduceComp xs
removeZeros :: [SimpleUnit] -> [SimpleUnit]
removeZeros [] = []
removeZeros (SimpleUnit _ _ 0.0 : xs) = removeZeros xs
removeZeros (x:xs) = x : removeZeros xs
invertUnits :: [SimpleUnit] -> [SimpleUnit]
invertUnits = map invertSimpleUnit
invertSimpleUnit :: SimpleUnit -> SimpleUnit
invertSimpleUnit (SimpleUnit s pr p) = SimpleUnit s pr (-p)
multiplyQuants :: (Num a) => Quantity a -> Quantity a -> Quantity a
multiplyQuants x y = reduceUnits $ Quantity mag newUnits
where mag = magnitude x * magnitude y
newUnits = (units x) { sUnits = units' x ++ units' y }
divideQuants :: (Fractional a) => Quantity a -> Quantity a -> Quantity a
divideQuants x y = reduceUnits $ Quantity mag newUnits
where mag = magnitude x / magnitude y
newUnits = (units x) { sUnits = units' x ++ invertUnits (units' y) }
exptQuants :: (Real a, Floating a) => Quantity a -> a -> Quantity a
exptQuants (Quantity x u) y = reduceUnits $ Quantity (x**y) newUnits
where expUnits = map (\(SimpleUnit s pr p) -> SimpleUnit s pr (p * realToFrac y))
newUnits = u { sUnits = expUnits (sUnits u) }
data Definition = PrefixDefinition { defPrefix :: Symbol
, factor :: Double
, defSynonyms :: [Symbol]}
| BaseDefinition { base :: Symbol
, dimBase :: Symbol
, defSynonyms ::[Symbol]}
| UnitDefinition { defSymbol :: Symbol
, quantity :: Quantity Double
, defSynonyms :: [Symbol]} deriving (Show, Eq, Ord)
data Definitions = Definitions { bases :: M.Map String (Double, [SimpleUnit])
, synonyms :: M.Map String String
, unitsList :: [String]
, prefixes :: [String]
, prefixValues :: M.Map String Double
, prefixSynonyms :: M.Map String String
, unitTypes :: M.Map String String
, defStringHash :: Int
} deriving (Show, Ord)
instance Eq Definitions where
d1 == d2 = defStringHash d1 == defStringHash d2
emptyDefinitions :: Definitions
emptyDefinitions = Definitions { bases = M.empty
, synonyms = M.empty
, unitsList = []
, prefixes = []
, prefixValues = M.fromList [("", 1)]
, prefixSynonyms = M.fromList [("", "")]
, unitTypes = M.empty
, defStringHash = -1 }
unionDefinitions :: Definitions -> Definitions -> Definitions
unionDefinitions d1 d2 = Definitions {
bases = bases d1 `M.union` bases d2
, synonyms = synonyms d1 `M.union` synonyms d2
, unitsList = unitsList d1 ++ unitsList d2
, prefixes = prefixes d1 ++ prefixes d2
, prefixValues = prefixValues d1 `M.union` prefixValues d2
, prefixSynonyms = prefixSynonyms d1 `M.union` prefixSynonyms d2
, unitTypes = unitTypes d1 `M.union` unitTypes d2
, defStringHash = -1 }