{-# OPTIONS_HADDOCK show-extensions #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE PatternGuards #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RoleAnnotations #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {- | Copyright : Copyright (C) 2006-2018 Bjorn Buckwalter License : BSD3 Maintainer : bjorn@buckwalter.se Stability : Stable Portability: GHC only = Summary In this module we provide data types for performing arithmetic with physical quantities and units. Information about the physical dimensions of the quantities/units is embedded in their types and the validity of operations is verified by the type checker at compile time. The boxing and unboxing of numerical values as quantities is done by multiplication and division of units, of which an incomplete set is provided. We limit ourselves to \"Newtonian\" physics. We do not attempt to accommodate relativistic physics in which e.g. addition of length and time would be valid. As far as possible and/or practical the conventions and guidelines of NIST's "Guide for the Use of the International System of Units (SI)" <#note1 [1]> are followed. Occasionally we will reference specific sections from the guide and deviations will be explained. == Disclaimer Merely an engineer, the author doubtlessly uses a language and notation that makes mathematicians and physicist cringe. He does not mind constructive criticism (or pull requests). The sets of functions and units defined herein are incomplete and reflect only the author's needs to date. Again, patches are welcome. = Usage == Preliminaries This module requires GHC 8 or later. We utilize Data Kinds, TypeNats, Closed Type Families, etc. Clients of the module are generally not required to use these extensions. Clients probably will want to use the NegativeLiterals extension. == Examples We have defined operators and units that allow us to define and work with physical quantities. A physical quantity is defined by multiplying a number with a unit (the type signature is optional). > v :: Velocity Prelude.Double > v = 90 *~ (kilo meter / hour) It follows naturally that the numerical value of a quantity is obtained by division by a unit. > numval :: Prelude.Double > numval = v /~ (meter / second) The notion of a quantity as the product of a numerical value and a unit is supported by 7.1 "Value and numerical value of a quantity" of <#note1 [1]>. While the above syntax is fairly natural it is unfortunate that it must violate a number of the guidelines in <#note1 [1]>, in particular 9.3 "Spelling unit names with prefixes", 9.4 "Spelling unit names obtained by multiplication", 9.5 "Spelling unit names obtained by division". As a more elaborate example of how to use the module we define a function for calculating the escape velocity of a celestial body <#note2 [2]>. > escapeVelocity :: (Floating a) => Mass a -> Length a -> Velocity a > escapeVelocity m r = sqrt (two * g * m / r) > where > two = 2 *~ one > g = 6.6720e-11 *~ (newton * meter ^ pos2 / kilo gram ^ pos2) For completeness we should also show an example of the error messages we will get from GHC when performing invalid arithmetic. In the best case GHC will be able to use the type synonyms we have defined in its error messages. > let x = 1 *~ meter + 1 *~ second > > Couldn't match type 'Numeric.NumType.DK.Integers.Zero > with 'Numeric.NumType.DK.Integers.Pos1 > Expected type: Unit 'Metric DLength a > Actual type: Unit 'Metric DTime a > In the second argument of `(*~)', namely `second' > In the second argument of `(+)', namely `1 *~ second' In other cases the error messages aren't very friendly. > let x = 1 *~ meter / (1 *~ second) + 1 *~ kilo gram > > Couldn't match type 'Numeric.NumType.DK.Integers.Zero > with 'Numeric.NumType.DK.Integers.Neg1 > Expected type: Quantity DMass a > Actual type: Dimensional > ('DQuantity V.* 'DQuantity) (DLength / DTime) a > In the first argument of `(+)', namely `1 *~ meter / (1 *~ second)' > In the expression: 1 *~ meter / (1 *~ second) + 1 *~ kilo gram > In an equation for `x': > x = 1 *~ meter / (1 *~ second) + 1 *~ kilo gram It is the author's experience that the usefulness of the compiler error messages is more often than not limited to pinpointing the location of errors. = Notes == Future work While there is an insane amount of units in use around the world it is reasonable to provide those in relatively widespread use. Units outside of SI will most likely be added on an as-needed basis. Additional physics models could be implemented. See <#note3 [3]> for ideas. == Related work Henning Thielemann numeric prelude has a physical units library, however, checking of dimensions is dynamic rather than static. Aaron Denney has created a toy example of statically checked physical dimensions covering only length and time. HaskellWiki has pointers <#note4 [4]> to these. Also see Samuel Hoffstaetter's blog post <#note5 [5]> which uses techniques similar to this library. Libraries with similar functionality exist for other programming languages and may serve as inspiration. The author has found the Java library JScience <#note6 [6]> and the Fortress programming language <#note7 [7]> particularly noteworthy. == References 1. #note1# http://physics.nist.gov/Pubs/SP811/ 2. #note2# http://en.wikipedia.org/wiki/Escape_velocity 3. #note3# http://jscience.org/api/org/jscience/physics/models/package-summary.html 4. #note4# http://www.haskell.org/haskellwiki/Physical_units 5. #note5# http://liftm.wordpress.com/2007/06/03/scientificdimension-type-arithmetic-and-physical-units-in-haskell/ 6. #note6# http://jscience.org/ 7. #note7# http://research.sun.com/projects/plrg/fortress.pdf -} module Numeric.Units.Dimensional ( -- * Types -- $types Dimensional, Unit, Quantity, Metricality(..), -- * Physical Dimensions -- $dimensions Dimension (Dim), -- ** Dimension Arithmetic -- $dimension-arithmetic type (*), type (/), type (^), NRoot, Sqrt, Cbrt, Recip, -- ** Term Level Representation of Dimensions -- $dimension-terms Dimension' (Dim'), HasDimension(..), KnownDimension, -- * Dimensional Arithmetic (*~), (/~), (^), (^/), (**), (*), (/), (+), (-), negate, abs, signum, recip, nroot, sqrt, cbrt, -- ** Transcendental Functions exp, log, logBase, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, atan2, log1p, expm1, log1pexp, log1mexp, -- ** Operations on Collections -- $collections (*~~), (/~~), sum, mean, product, dimensionlessLength, nFromTo, -- * Dimension Synonyms -- $dimension-synonyms DOne, DLength, DMass, DTime, DElectricCurrent, DThermodynamicTemperature, DAmountOfSubstance, DLuminousIntensity, -- * Quantity Synonyms -- $quantity-synonyms Dimensionless, Length, Mass, Time, ElectricCurrent, ThermodynamicTemperature, AmountOfSubstance, LuminousIntensity, -- * Constants -- $constants _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, pi, tau, -- * Constructing Units siUnit, one, mkUnitR, mkUnitQ, mkUnitZ, -- * Unit Metadata name, exactValue, weaken, strengthen, exactify, -- * Pretty Printing showIn, -- * On 'Functor', and Conversion Between Number Representations -- $functor KnownVariant(dmap), changeRep, changeRepApproximate, -- * Lenses -- $lenses asLens ) where import Prelude ( Eq(..), Num, Fractional, Floating, Real, RealFloat, Functor, fmap , (.), flip, (++), fromIntegral, fromInteger, fromRational, error, max, succ , Int, Integer, Integral, ($), uncurry, realToFrac, otherwise ) import qualified Prelude import Numeric.NumType.DK.Integers ( pos2, pos3 , KnownTypeInt, toNum ) import Data.Data import Data.ExactPi import Data.Foldable (Foldable(foldr, length)) import Data.Maybe import Data.Ratio import qualified Numeric import Numeric.Units.Dimensional.Dimensions import Numeric.Units.Dimensional.Internal import Numeric.Units.Dimensional.UnitNames hiding ((*), (/), (^), weaken, strengthen, product) import qualified Numeric.Units.Dimensional.UnitNames.Internal as Name import Numeric.Units.Dimensional.Variants hiding (type (*), type (/)) import qualified Numeric.Units.Dimensional.Variants as V -- $setup -- >>> :set -XFlexibleInstances -- >>> :set -XNoImplicitPrelude -- >>> import Test.QuickCheck.Arbitrary -- >>> import Numeric.Units.Dimensional.Prelude -- >>> import Numeric.Units.Dimensional.Float -- >>> import Numeric.Units.Dimensional.NonSI -- >>> instance Arbitrary a => Arbitrary (Quantity d a) where arbitrary = fmap Quantity arbitrary {- We will reuse the operators and function names from the Prelude. To prevent unpleasant surprises we give operators the same fixity as the Prelude. -} infixr 8 ^, ^/, ** infixl 7 *, / infixl 6 +, - {- $types Our primary objective is to define a data type that can be used to represent (while still differentiating between) units and quantities. There are two reasons for consolidating units and quantities in one data type. The first being to allow code reuse as they are largely subject to the same operations. The second being that it allows reuse of operators (and functions) between the two without resorting to occasionally cumbersome type classes. The relationship between (the value of) a 'Quantity', its numerical value and its 'Unit' is described in 7.1 "Value and numerical value of a quantity" of <#note1 [1]>. In short a 'Quantity' is the product of a number and a 'Unit'. We define the '*~' operator as a convenient way to declare quantities as such a product. -} -- | Extracts the 'UnitName' of a 'Unit'. name :: Unit m d a -> UnitName m name :: Unit m d a -> UnitName m name (Unit n _ _) = UnitName m n -- | Extracts the exact value of a 'Unit', expressed in terms of the SI coherent derived unit (see 'siUnit') of the same 'Dimension'. -- -- Note that the actual value may in some cases be approximate, for example if the unit is defined by experiment. exactValue :: Unit m d a -> ExactPi exactValue :: Unit m d a -> ExactPi exactValue (Unit _ e _) = ExactPi e -- | Discards potentially unwanted type level information about a 'Unit'. weaken :: Unit m d a -> Unit 'NonMetric d a weaken :: Unit m d a -> Unit 'NonMetric d a weaken (Unit n e v) = UnitName 'NonMetric -> ExactPi -> a -> Unit 'NonMetric d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit (UnitName m -> UnitName 'NonMetric forall (m :: Metricality). UnitName m -> UnitName 'NonMetric Name.weaken UnitName m n) ExactPi e a v -- | Attempts to convert a 'Unit' which may or may not be 'Metric' to one -- which is certainly 'Metric'. strengthen :: Unit m d a -> Maybe (Unit 'Metric d a) strengthen :: Unit m d a -> Maybe (Unit 'Metric d a) strengthen (Unit n e v) | Just UnitName 'Metric n' <- UnitName m -> Maybe (UnitName 'Metric) forall (m :: Metricality). UnitName m -> Maybe (UnitName 'Metric) Name.strengthen UnitName m n = Unit 'Metric d a -> Maybe (Unit 'Metric d a) forall a. a -> Maybe a Just (Unit 'Metric d a -> Maybe (Unit 'Metric d a)) -> Unit 'Metric d a -> Maybe (Unit 'Metric d a) forall a b. (a -> b) -> a -> b $ UnitName 'Metric -> ExactPi -> a -> Unit 'Metric d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName 'Metric n' ExactPi e a v | Bool otherwise = Maybe (Unit 'Metric d a) forall a. Maybe a Nothing -- | Forms the exact version of a 'Unit'. exactify :: Unit m d a -> Unit m d ExactPi exactify :: Unit m d a -> Unit m d ExactPi exactify (Unit n e _) = UnitName m -> ExactPi -> ExactPi -> Unit m d ExactPi forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName m n ExactPi e ExactPi e -- | Forms a 'Quantity' by multipliying a number and a unit. (*~) :: (Num a) => a -> Unit m d a -> Quantity d a a x *~ :: a -> Unit m d a -> Quantity d a *~ (Unit _ _ y) = a -> Quantity d a forall (s :: ExactPi') (d :: Dimension) a. a -> Dimensional ('DQuantity s) d a Quantity (a x a -> a -> a forall a. Num a => a -> a -> a Prelude.* a y) -- | Divides a 'Quantity' by a 'Unit' of the same physical dimension, obtaining the -- numerical value of the quantity expressed in that unit. (/~) :: Fractional a => Quantity d a -> Unit m d a -> a (Quantity x) /~ :: Quantity d a -> Unit m d a -> a /~ (Unit _ _ y) = (a x a -> a -> a forall a. Fractional a => a -> a -> a Prelude./ a y) {- We give '*~' and '/~' the same fixity as '*' and '/' defined below. Note that this necessitates the use of parenthesis when composing units using '*' and '/', e.g. "1 *~ (meter / second)". -} infixl 7 *~, /~ {- $dimensions The phantom type variable d encompasses the physical dimension of a 'Dimensional'. As detailed in <#note5 [5]> there are seven base dimensions, which can be combined in integer powers to a given physical dimension. We represent physical dimensions as the powers of the seven base dimensions that make up the given dimension. The powers are represented using NumTypes. For convenience we collect all seven base dimensions in a data kind 'Dimension'. We could have chosen to provide type variables for the seven base dimensions in 'Dimensional' instead of creating a new data kind 'Dimension'. However, that would have made any type signatures involving 'Dimensional' very cumbersome. By encompassing the physical dimension in a single type variable we can "hide" the cumbersome type arithmetic behind convenient type classes as will be seen later. -} {- $dimension-synonyms Using our 'Dimension' data kind we define some type synonyms for convenience. We start with the base dimensions, others can be found in "Numeric.Units.Dimensional.Quantities". -} {- $quantity-synonyms Using the above type synonyms we can define type synonyms for quantities of particular physical dimensions. Again we limit ourselves to the base dimensions, others can be found in "Numeric.Units.Dimensional.Quantities". -} type Dimensionless = Quantity DOne type Length = Quantity DLength type Mass = Quantity DMass type Time = Quantity DTime type ElectricCurrent = Quantity DElectricCurrent type ThermodynamicTemperature = Quantity DThermodynamicTemperature type AmountOfSubstance = Quantity DAmountOfSubstance type LuminousIntensity = Quantity DLuminousIntensity {- $dimension-arithmetic When performing arithmetic on units and quantities the arithmetics must be applied to both the numerical values of the Dimensionals but also to their physical dimensions. The type level arithmetic on physical dimensions is governed by closed type families expressed as type operators. We could provide the 'Mul' and 'Div' classes with full functional dependencies but that would be of limited utility as there is no limited use for "backwards" type inference. Efforts are underway to develop a type-checker plugin that does enable these scenarios, e.g. for linear algebra. -} {- = Arithmetic on units and quantities = Thanks to the arithmetic on physical dimensions having been sorted out separately a lot of the arithmetic on Dimensionals is straight forward. In particular the type signatures are much simplified. Multiplication, division and powers apply to both units and quantities. -} -- | Multiplies two 'Quantity's or two 'Unit's. -- -- The intimidating type signature captures the similarity between these operations -- and ensures that composite 'Unit's are 'NonMetric'. (*) :: (KnownVariant v1, KnownVariant v2, KnownVariant (v1 V.* v2), Num a) => Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 V.* v2) (d1 * d2) a * :: Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 * v2) (d1 * d2) a (*) = (ExactPi -> ExactPi -> ExactPi) -> (a -> a -> a) -> UnitNameTransformer2 -> Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 * v2) (d1 * d2) a forall (v1 :: Variant) (v2 :: Variant) (v3 :: Variant) a (d1 :: Dimension) (d2 :: Dimension) (d3 :: Dimension). (KnownVariant v1, KnownVariant v2, KnownVariant v3) => (ExactPi -> ExactPi -> ExactPi) -> (a -> a -> a) -> UnitNameTransformer2 -> Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional v3 d3 a liftD2 ExactPi -> ExactPi -> ExactPi forall a. Num a => a -> a -> a (Prelude.*) a -> a -> a forall a. Num a => a -> a -> a (Prelude.*) UnitNameTransformer2 (Name.*) -- | Divides one 'Quantity' by another or one 'Unit' by another. -- -- The intimidating type signature captures the similarity between these operations -- and ensures that composite 'Unit's are 'NotPrefixable'. (/) :: (KnownVariant v1, KnownVariant v2, KnownVariant (v1 V./ v2), Fractional a) => Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 V./ v2) (d1 / d2) a / :: Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 / v2) (d1 / d2) a (/) = (ExactPi -> ExactPi -> ExactPi) -> (a -> a -> a) -> UnitNameTransformer2 -> Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 / v2) (d1 / d2) a forall (v1 :: Variant) (v2 :: Variant) (v3 :: Variant) a (d1 :: Dimension) (d2 :: Dimension) (d3 :: Dimension). (KnownVariant v1, KnownVariant v2, KnownVariant v3) => (ExactPi -> ExactPi -> ExactPi) -> (a -> a -> a) -> UnitNameTransformer2 -> Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional v3 d3 a liftD2 ExactPi -> ExactPi -> ExactPi forall a. Fractional a => a -> a -> a (Prelude./) a -> a -> a forall a. Fractional a => a -> a -> a (Prelude./) UnitNameTransformer2 (Name./) -- | Forms the reciprocal of a 'Quantity', which has the reciprocal dimension. -- -- >>> recip $ 47 *~ hertz -- 2.127659574468085e-2 s recip :: (Fractional a) => Quantity d a -> Quantity (Recip d) a recip :: Quantity d a -> Quantity (Recip d) a recip = (a -> a) -> Quantity d a -> Quantity (Recip d) a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension). (a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a liftQ a -> a forall a. Fractional a => a -> a Prelude.recip -- | Raises a 'Quantity' or 'Unit' to an integer power. -- -- Because the power chosen impacts the 'Dimension' of the result, it is necessary to supply a type-level representation -- of the exponent in the form of a 'Proxy' to some 'TypeInt'. Convenience values 'pos1', 'pos2', 'neg1', ... -- are supplied by the "Numeric.NumType.DK.Integers" module. The most commonly used ones are -- also reexported by "Numeric.Units.Dimensional.Prelude". -- -- The intimidating type signature captures the similarity between these operations -- and ensures that composite 'Unit's are 'NotPrefixable'. (^) :: (Fractional a, KnownTypeInt i, KnownVariant v, KnownVariant (Weaken v)) => Dimensional v d1 a -> Proxy i -> Dimensional (Weaken v) (d1 ^ i) a Dimensional v d1 a x ^ :: Dimensional v d1 a -> Proxy i -> Dimensional (Weaken v) (d1 ^ i) a ^ Proxy i n = let n' :: Int n' = (Proxy i -> Int forall (i :: TypeInt) a. (KnownTypeInt i, Num a) => Proxy i -> a toNum Proxy i n) :: Int in (ExactPi -> ExactPi) -> (a -> a) -> (forall (m :: Metricality). UnitName m -> UnitName 'NonMetric) -> Dimensional v d1 a -> Dimensional (Weaken v) (d1 ^ i) a forall (v1 :: Variant) (v2 :: Variant) a b (d1 :: Dimension) (d2 :: Dimension). (KnownVariant v1, KnownVariant v2) => (ExactPi -> ExactPi) -> (a -> b) -> (forall (m :: Metricality). UnitName m -> UnitName 'NonMetric) -> Dimensional v1 d1 a -> Dimensional v2 d2 b liftD (ExactPi -> Int -> ExactPi forall a b. (Fractional a, Integral b) => a -> b -> a Prelude.^^ Int n') (a -> Int -> a forall a b. (Fractional a, Integral b) => a -> b -> a Prelude.^^ Int n') (UnitName m -> Int -> UnitName 'NonMetric forall (m :: Metricality). UnitName m -> Int -> UnitName 'NonMetric Name.^ Int n') Dimensional v d1 a x {- A special case is that dimensionless quantities are not restricted to integer exponents. This is accommodated by the '**' operator defined later. = Quantity operations = Some additional operations obviously only make sense for quantities. Of these, negation, addition and subtraction are particularly simple as they are done in a single physical dimension. -} -- | Negates the value of a 'Quantity'. negate :: Num a => Quantity d a -> Quantity d a negate :: Quantity d a -> Quantity d a negate = (a -> a) -> Quantity d a -> Quantity d a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension). (a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a liftQ a -> a forall a. Num a => a -> a Prelude.negate -- | Adds two 'Quantity's. (+) :: Num a => Quantity d a -> Quantity d a -> Quantity d a + :: Quantity d a -> Quantity d a -> Quantity d a (+) = (a -> a -> a) -> Quantity d a -> Quantity d a -> Quantity d a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension) (s3 :: ExactPi') (d3 :: Dimension). (a -> a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a -> SQuantity s3 d3 a liftQ2 a -> a -> a forall a. Num a => a -> a -> a (Prelude.+) -- | Subtracts one 'Quantity' from another. (-) :: Num a => Quantity d a -> Quantity d a -> Quantity d a (-) = (a -> a -> a) -> Quantity d a -> Quantity d a -> Quantity d a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension) (s3 :: ExactPi') (d3 :: Dimension). (a -> a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a -> SQuantity s3 d3 a liftQ2 a -> a -> a forall a. Num a => a -> a -> a (Prelude.-) -- | Takes the absolute value of a 'Quantity'. abs :: Num a => Quantity d a -> Quantity d a abs :: Quantity d a -> Quantity d a abs = (a -> a) -> Quantity d a -> Quantity d a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension). (a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a liftQ a -> a forall a. Num a => a -> a Prelude.abs -- | Takes the sign of a 'Quantity'. The functions 'abs' and 'signum' -- satisy the law that: -- -- > abs x * signum x == x -- -- The sign is either @negate _1@ (negative), @_0@ (zero), -- or @_1@ (positive). signum :: Num a => Quantity d a -> Dimensionless a signum :: Quantity d a -> Dimensionless a signum = (a -> a) -> Quantity d a -> Dimensionless a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension). (a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a liftQ a -> a forall a. Num a => a -> a Prelude.signum {- Roots of arbitrary (integral) degree. Appears to occasionally be useful for units as well as quantities. -} -- | Computes the nth root of a 'Quantity' using 'Prelude.**'. -- -- The 'NRoot' type family will prevent application of this operator where the result would have a fractional dimension or where n is zero. -- -- Because the root chosen impacts the 'Dimension' of the result, it is necessary to supply a type-level representation -- of the root in the form of a 'Proxy' to some 'TypeInt'. Convenience values 'pos1', 'pos2', 'neg1', ... -- are supplied by the "Numeric.NumType.DK.Integers" module. The most commonly used ones are -- also reexported by "Numeric.Units.Dimensional.Prelude". -- -- n must not be zero. Negative roots are defined such that @nroot (Proxy :: Proxy (Negate n)) x == nroot (Proxy :: Proxy n) (recip x)@. -- -- Also available in operator form, see '^/'. nroot :: (KnownTypeInt n, Floating a) => Proxy n -> Quantity d a -> Quantity (NRoot d n) a nroot :: Proxy n -> Quantity d a -> Quantity (NRoot d n) a nroot Proxy n n = let n' :: a n' = a 1 a -> a -> a forall a. Fractional a => a -> a -> a Prelude./ Proxy n -> a forall (i :: TypeInt) a. (KnownTypeInt i, Num a) => Proxy i -> a toNum Proxy n n in (a -> a) -> Quantity d a -> Quantity (NRoot d n) a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension). (a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a liftQ (a -> a -> a forall a. Floating a => a -> a -> a Prelude.** a n') {- We provide short-hands for the square and cube roots. -} -- | Computes the square root of a 'Quantity' using 'Prelude.**'. -- -- The 'NRoot' type family will prevent application where the supplied quantity does not have a square dimension. -- -- prop> (x :: Area Double) >= _0 ==> sqrt x == nroot pos2 x sqrt :: Floating a => Quantity d a -> Quantity (Sqrt d) a sqrt :: Quantity d a -> Quantity (Sqrt d) a sqrt = Proxy 'Pos2 -> Quantity d a -> Quantity (Sqrt d) a forall (n :: TypeInt) a (d :: Dimension). (KnownTypeInt n, Floating a) => Proxy n -> Quantity d a -> Quantity (NRoot d n) a nroot Proxy 'Pos2 pos2 -- | Computes the cube root of a 'Quantity' using 'Prelude.**'. -- -- The 'NRoot' type family will prevent application where the supplied quantity does not have a cubic dimension. -- -- prop> (x :: Volume Double) >= _0 ==> cbrt x == nroot pos3 x cbrt :: Floating a => Quantity d a -> Quantity (Cbrt d) a cbrt :: Quantity d a -> Quantity (Cbrt d) a cbrt = Proxy 'Pos3 -> Quantity d a -> Quantity (Cbrt d) a forall (n :: TypeInt) a (d :: Dimension). (KnownTypeInt n, Floating a) => Proxy n -> Quantity d a -> Quantity (NRoot d n) a nroot Proxy 'Pos3 pos3 {- We also provide an operator alternative to nroot for those that prefer such. -} -- | Computes the nth root of a 'Quantity' using 'Prelude.**'. -- -- The 'NRoot' type family will prevent application of this operator where the result would have a fractional dimension or where n is zero. -- -- Because the root chosen impacts the 'Dimension' of the result, it is necessary to supply a type-level representation -- of the root in the form of a 'Proxy' to some 'TypeInt'. Convenience values 'pos1', 'pos2', 'neg1', ... -- are supplied by the "Numeric.NumType.DK.Integers" module. The most commonly used ones are -- also reexported by "Numeric.Units.Dimensional.Prelude". -- -- Also available in prefix form, see 'nroot'. (^/) :: (KnownTypeInt n, Floating a) => Quantity d a -> Proxy n -> Quantity (NRoot d n) a ^/ :: Quantity d a -> Proxy n -> Quantity (NRoot d n) a (^/) = (Proxy n -> Quantity d a -> Quantity (NRoot d n) a) -> Quantity d a -> Proxy n -> Quantity (NRoot d n) a forall a b c. (a -> b -> c) -> b -> a -> c flip Proxy n -> Quantity d a -> Quantity (NRoot d n) a forall (n :: TypeInt) a (d :: Dimension). (KnownTypeInt n, Floating a) => Proxy n -> Quantity d a -> Quantity (NRoot d n) a nroot {- $collections Here we define operators and functions to make working with homogenuous lists of dimensionals more convenient. We define two convenience operators for applying units to all elements of a functor (e.g. a list). -} -- | Applies '*~' to all values in a functor. (*~~) :: (Functor f, Num a) => f a -> Unit m d a -> f (Quantity d a) f a xs *~~ :: f a -> Unit m d a -> f (Quantity d a) *~~ Unit m d a u = (a -> Quantity d a) -> f a -> f (Quantity d a) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap (a -> Unit m d a -> Quantity d a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit m d a u) f a xs -- | Applies '/~' to all values in a functor. (/~~) :: forall f m d a.(Functor f, Fractional a) => f (Quantity d a) -> Unit m d a -> f a f (Quantity d a) xs /~~ :: f (Quantity d a) -> Unit m d a -> f a /~~ Unit m d a u = (Quantity d a -> a) -> f (Quantity d a) -> f a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap (Quantity d a -> Unit m d a -> a forall a (d :: Dimension) (m :: Metricality). Fractional a => Quantity d a -> Unit m d a -> a /~ Unit m d a u) f (Quantity d a) xs infixl 7 *~~, /~~ -- | The sum of all elements in a foldable structure. -- -- >>> sum ([] :: [Mass Double]) -- 0.0 kg -- -- >>> sum [12.4 *~ meter, 1 *~ foot] -- 12.7048 m sum :: (Num a, Foldable f) => f (Quantity d a) -> Quantity d a sum :: f (Quantity d a) -> Quantity d a sum = (Quantity d a -> Quantity d a -> Quantity d a) -> Quantity d a -> f (Quantity d a) -> Quantity d a forall (t :: Type -> Type) a b. Foldable t => (a -> b -> b) -> b -> t a -> b foldr Quantity d a -> Quantity d a -> Quantity d a forall a (d :: Dimension). Num a => Quantity d a -> Quantity d a -> Quantity d a (+) Quantity d a forall a (d :: Dimension). Num a => Quantity d a _0 -- | The product of all elements in a foldable structure. -- -- >>> product ([] :: [Dimensionless Double]) -- 1.0 -- -- >>> product [pi, _4, 0.36 *~ one] -- 4.523893421169302 product :: (Num a, Foldable f) => f (Dimensionless a) -> Dimensionless a product :: f (Dimensionless a) -> Dimensionless a product = (Dimensionless a -> Dimensionless a -> Dimensionless a) -> Dimensionless a -> f (Dimensionless a) -> Dimensionless a forall (t :: Type -> Type) a b. Foldable t => (a -> b -> b) -> b -> t a -> b foldr Dimensionless a -> Dimensionless a -> Dimensionless a forall (v1 :: Variant) (v2 :: Variant) a (d1 :: Dimension) (d2 :: Dimension). (KnownVariant v1, KnownVariant v2, KnownVariant (v1 * v2), Num a) => Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 * v2) (d1 * d2) a (*) Dimensionless a forall a. Num a => Dimensionless a _1 -- | The arithmetic mean of all elements in a foldable structure. -- -- >>> mean [pi, _7] -- 5.070796326794897 mean :: (Fractional a, Foldable f) => f (Quantity d a) -> Quantity d a mean :: f (Quantity d a) -> Quantity d a mean = (Quantity d a -> Dimensional ('DQuantity One) DOne a -> Quantity d a) -> (Quantity d a, Dimensional ('DQuantity One) DOne a) -> Quantity d a forall a b c. (a -> b -> c) -> (a, b) -> c uncurry Quantity d a -> Dimensional ('DQuantity One) DOne a -> Quantity d a forall (v1 :: Variant) (v2 :: Variant) a (d1 :: Dimension) (d2 :: Dimension). (KnownVariant v1, KnownVariant v2, KnownVariant (v1 / v2), Fractional a) => Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 / v2) (d1 / d2) a (/) ((Quantity d a, Dimensional ('DQuantity One) DOne a) -> Quantity d a) -> (f (Quantity d a) -> (Quantity d a, Dimensional ('DQuantity One) DOne a)) -> f (Quantity d a) -> Quantity d a forall b c a. (b -> c) -> (a -> b) -> a -> c . (Quantity d a -> (Quantity d a, Dimensional ('DQuantity One) DOne a) -> (Quantity d a, Dimensional ('DQuantity One) DOne a)) -> (Quantity d a, Dimensional ('DQuantity One) DOne a) -> f (Quantity d a) -> (Quantity d a, Dimensional ('DQuantity One) DOne a) forall (t :: Type -> Type) a b. Foldable t => (a -> b -> b) -> b -> t a -> b foldr Quantity d a -> (Quantity d a, Dimensional ('DQuantity One) DOne a) -> (Quantity d a, Dimensional ('DQuantity One) DOne a) forall a a (d :: Dimension). (Num a, Num a) => Quantity d a -> (Quantity d a, Quantity DOne a) -> (Quantity d a, Quantity DOne a) accumulate (Quantity d a forall a (d :: Dimension). Num a => Quantity d a _0, Dimensional ('DQuantity One) DOne a forall a (d :: Dimension). Num a => Quantity d a _0) where accumulate :: Quantity d a -> (Quantity d a, Quantity DOne a) -> (Quantity d a, Quantity DOne a) accumulate Quantity d a val (Quantity d a accum, Quantity DOne a count) = (Quantity d a accum Quantity d a -> Quantity d a -> Quantity d a forall a (d :: Dimension). Num a => Quantity d a -> Quantity d a -> Quantity d a + Quantity d a val, Quantity DOne a count Quantity DOne a -> Quantity DOne a -> Quantity DOne a forall a (d :: Dimension). Num a => Quantity d a -> Quantity d a -> Quantity d a + Quantity DOne a forall a. Num a => Dimensionless a _1) -- | The length of the foldable data structure as a 'Dimensionless'. -- This can be useful for purposes of e.g. calculating averages. -- -- >>> dimensionlessLength ["foo", "bar"] -- 2 dimensionlessLength :: (Num a, Foldable f) => f b -> Dimensionless a dimensionlessLength :: f b -> Dimensionless a dimensionlessLength f b x = (Int -> a forall a b. (Integral a, Num b) => a -> b fromIntegral (Int -> a) -> Int -> a forall a b. (a -> b) -> a -> b $ f b -> Int forall (t :: Type -> Type) a. Foldable t => t a -> Int length f b x) a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one -- | Returns a list of quantities between given bounds. -- -- prop> n <= 0 ==> nFromTo (x :: Mass Double) (y :: Mass Double) n == [x, y] -- -- prop> (x :: Length Double) <= (y :: Length Double) ==> all (\z -> x <= z && z <= y) (nFromTo x y n) -- -- >>> nFromTo _0 _3 2 -- [0.0,1.0,2.0,3.0] -- -- >>> nFromTo _1 _0 7 -- [1.0,0.875,0.75,0.625,0.5,0.375,0.25,0.125,0.0] -- -- >>> nFromTo _0 _1 (-5) -- [0.0,1.0] nFromTo :: (Fractional a, Integral b) => Quantity d a -- ^ The initial value. -> Quantity d a -- ^ The final value. -> b -- ^ The number of intermediate values. If less than one, no intermediate values will result. -> [Quantity d a] nFromTo :: Quantity d a -> Quantity d a -> b -> [Quantity d a] nFromTo Quantity d a xi Quantity d a xf b n = (b -> Quantity d a) -> [b] -> [Quantity d a] forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap b -> Quantity d a f [b 0..b n'] [Quantity d a] -> [Quantity d a] -> [Quantity d a] forall a. [a] -> [a] -> [a] ++ [Quantity d a xf] where n' :: b n' = b -> b -> b forall a. Ord a => a -> a -> a max b 0 b n f :: b -> Quantity d a f b i = Quantity d a xi Quantity d a -> Quantity d a -> Quantity d a forall a (d :: Dimension). Num a => Quantity d a -> Quantity d a -> Quantity d a + Ratio b -> a forall a b. (Real a, Fractional b) => a -> b realToFrac (b i b -> b -> Ratio b forall a. Integral a => a -> a -> Ratio a % b -> b forall a. Enum a => a -> a succ b n') a -> Unit 'NonMetric DOne a -> Quantity DOne a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one Quantity DOne a -> Quantity d a -> Dimensional ('DQuantity One * 'DQuantity One) (DOne * d) a forall (v1 :: Variant) (v2 :: Variant) a (d1 :: Dimension) (d2 :: Dimension). (KnownVariant v1, KnownVariant v2, KnownVariant (v1 * v2), Num a) => Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 * v2) (d1 * d2) a * (Quantity d a xf Quantity d a -> Quantity d a -> Quantity d a forall a (d :: Dimension). Num a => Quantity d a -> Quantity d a -> Quantity d a - Quantity d a xi) {- We continue by defining elementary functions on 'Dimensionless' that may be obviously useful. -} exp, log, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, log1p, expm1, log1pexp, log1mexp :: Floating a => Dimensionless a -> Dimensionless a exp :: Dimensionless a -> Dimensionless a exp = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.exp log :: Dimensionless a -> Dimensionless a log = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.log sin :: Dimensionless a -> Dimensionless a sin = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.sin cos :: Dimensionless a -> Dimensionless a cos = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.cos tan :: Dimensionless a -> Dimensionless a tan = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.tan asin :: Dimensionless a -> Dimensionless a asin = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.asin acos :: Dimensionless a -> Dimensionless a acos = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.acos atan :: Dimensionless a -> Dimensionless a atan = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.atan sinh :: Dimensionless a -> Dimensionless a sinh = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.sinh cosh :: Dimensionless a -> Dimensionless a cosh = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.cosh tanh :: Dimensionless a -> Dimensionless a tanh = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.tanh asinh :: Dimensionless a -> Dimensionless a asinh = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.asinh acosh :: Dimensionless a -> Dimensionless a acosh = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.acosh atanh :: Dimensionless a -> Dimensionless a atanh = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Prelude.atanh log1p :: Dimensionless a -> Dimensionless a log1p = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Numeric.log1p expm1 :: Dimensionless a -> Dimensionless a expm1 = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Numeric.expm1 log1pexp :: Dimensionless a -> Dimensionless a log1pexp = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Numeric.log1pexp log1mexp :: Dimensionless a -> Dimensionless a log1mexp = (a -> a) -> Dimensionless a -> Dimensionless a forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap a -> a forall a. Floating a => a -> a Numeric.log1mexp -- | Raises a dimensionless quantity to a dimensionless power. (**) :: Floating a => Dimensionless a -> Dimensionless a -> Dimensionless a ** :: Dimensionless a -> Dimensionless a -> Dimensionless a (**) = (a -> a -> a) -> Dimensionless a -> Dimensionless a -> Dimensionless a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension) (s3 :: ExactPi') (d3 :: Dimension). (a -> a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a -> SQuantity s3 d3 a liftQ2 a -> a -> a forall a. Floating a => a -> a -> a (Prelude.**) -- | Takes the logarithm of the second argument in the base of the first. -- -- >>> logBase _2 _8 -- 3.0 logBase :: Floating a => Dimensionless a -> Dimensionless a -> Dimensionless a logBase :: Dimensionless a -> Dimensionless a -> Dimensionless a logBase = (a -> a -> a) -> Dimensionless a -> Dimensionless a -> Dimensionless a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension) (s3 :: ExactPi') (d3 :: Dimension). (a -> a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a -> SQuantity s3 d3 a liftQ2 a -> a -> a forall a. Floating a => a -> a -> a Prelude.logBase -- | The standard two argument arctangent function. -- Since it interprets its two arguments in comparison with one another, the input may have any dimension. -- -- >>> atan2 _0 _1 -- 0.0 -- -- >>> atan2 _1 _0 -- 1.5707963267948966 -- -- >>> atan2 _0 (negate _1) -- 3.141592653589793 -- -- >>> atan2 (negate _1) _0 -- -1.5707963267948966 atan2 :: (RealFloat a) => Quantity d a -> Quantity d a -> Dimensionless a atan2 :: Quantity d a -> Quantity d a -> Dimensionless a atan2 = (a -> a -> a) -> Quantity d a -> Quantity d a -> Dimensionless a forall a (s1 :: ExactPi') (d1 :: Dimension) (s2 :: ExactPi') (d2 :: Dimension) (s3 :: ExactPi') (d3 :: Dimension). (a -> a -> a) -> SQuantity s1 d1 a -> SQuantity s2 d2 a -> SQuantity s3 d3 a liftQ2 a -> a -> a forall a. RealFloat a => a -> a -> a Prelude.atan2 {- The only unit we will define in this module is 'one'. -} -- | The unit 'one' has dimension 'DOne' and is the base unit of dimensionless values. -- -- As detailed in 7.10 "Values of quantities expressed simply as numbers: -- the unit one, symbol 1" of <#note1 [1]> the unit one generally does not -- appear in expressions. However, for us it is necessary to use 'one' -- as we would any other unit to perform the "boxing" of dimensionless values. one :: Num a => Unit 'NonMetric DOne a one :: Unit 'NonMetric DOne a one = UnitName 'NonMetric -> ExactPi -> a -> Unit 'NonMetric DOne a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName 'NonMetric nOne ExactPi 1 a 1 {- $constants For convenience we define some constants for small integer values that often show up in formulae. We also throw in 'pi' and 'tau' for good measure. -} -- | The constant for zero is polymorphic, allowing it to express zero 'Length' or -- 'Numeric.Units.Dimensional.Quantities.Capacitance' or 'Numeric.Units.Dimensional.Quantities.Velocity' etc, -- in addition to the 'Dimensionless' value zero. _0 :: Num a => Quantity d a _0 :: Quantity d a _0 = a -> Quantity d a forall (s :: ExactPi') (d :: Dimension) a. a -> Dimensional ('DQuantity s) d a Quantity a 0 _1, _2, _3, _4, _5, _6, _7, _8, _9 :: (Num a) => Dimensionless a _1 :: Dimensionless a _1 = a 1 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _2 :: Dimensionless a _2 = a 2 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _3 :: Dimensionless a _3 = a 3 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _4 :: Dimensionless a _4 = a 4 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _5 :: Dimensionless a _5 = a 5 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _6 :: Dimensionless a _6 = a 6 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _7 :: Dimensionless a _7 = a 7 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _8 :: Dimensionless a _8 = a 8 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one _9 :: Dimensionless a _9 = a 9 a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one pi :: Floating a => Dimensionless a pi :: Dimensionless a pi = a forall a. Floating a => a Prelude.pi a -> Unit 'NonMetric DOne a -> Dimensionless a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit 'NonMetric DOne a forall a. Num a => Unit 'NonMetric DOne a one -- | Twice 'pi'. -- -- For background on 'tau' see http://tauday.com/tau-manifesto (but also -- feel free to review http://www.thepimanifesto.com). tau :: Floating a => Dimensionless a tau :: Dimensionless a tau = Dimensionless a forall a. Num a => Dimensionless a _2 Dimensionless a -> Dimensionless a -> Dimensional ('DQuantity One * 'DQuantity One) (DOne * DOne) a forall (v1 :: Variant) (v2 :: Variant) a (d1 :: Dimension) (d2 :: Dimension). (KnownVariant v1, KnownVariant v2, KnownVariant (v1 * v2), Num a) => Dimensional v1 d1 a -> Dimensional v2 d2 a -> Dimensional (v1 * v2) (d1 * d2) a * Dimensionless a forall a. Floating a => Dimensionless a pi {- $functor We intentionally decline to provide a 'Functor' instance for 'Dimensional' because its use breaks the abstraction of physical dimensions. If you feel your work requires this instance, it is provided as an orphan in "Numeric.Units.Dimensional.Functor". -} -- | Convenient conversion between numerical types while retaining dimensional information. -- -- >>> let x = (37 :: Rational) *~ poundMass -- >>> changeRep x :: Mass Double -- 16.78291769 kg changeRep :: (KnownVariant v, Real a, Fractional b) => Dimensional v d a -> Dimensional v d b changeRep :: Dimensional v d a -> Dimensional v d b changeRep = (a -> b) -> Dimensional v d a -> Dimensional v d b forall (v :: Variant) a1 a2 (d :: Dimension). KnownVariant v => (a1 -> a2) -> Dimensional v d a1 -> Dimensional v d a2 dmap a -> b forall a b. (Real a, Fractional b) => a -> b realToFrac -- | Convenient conversion from exactly represented values while retaining dimensional information. changeRepApproximate :: (KnownVariant v, Floating b) => Dimensional v d ExactPi -> Dimensional v d b changeRepApproximate :: Dimensional v d ExactPi -> Dimensional v d b changeRepApproximate = (ExactPi -> b) -> Dimensional v d ExactPi -> Dimensional v d b forall (v :: Variant) a1 a2 (d :: Dimension). KnownVariant v => (a1 -> a2) -> Dimensional v d a1 -> Dimensional v d a2 dmap ExactPi -> b forall a. Floating a => ExactPi -> a approximateValue {- $lenses These functions are compatible with the lens library. -} -- | Converts a 'Unit' into a lens from 'Quantity's to values. asLens :: (Fractional a) => Unit m d a -> (forall f.Functor f => (a -> f a) -> Quantity d a -> f (Quantity d a)) asLens :: Unit m d a -> forall (f :: Type -> Type). Functor f => (a -> f a) -> Quantity d a -> f (Quantity d a) asLens Unit m d a u a -> f a f Quantity d a q = (a -> Quantity d a) -> f a -> f (Quantity d a) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b fmap (\a v' -> a v' a -> Unit m d a -> Quantity d a forall a (m :: Metricality) (d :: Dimension). Num a => a -> Unit m d a -> Quantity d a *~ Unit m d a u) (a -> f a f (Quantity d a q Quantity d a -> Unit m d a -> a forall a (d :: Dimension) (m :: Metricality). Fractional a => Quantity d a -> Unit m d a -> a /~ Unit m d a u)) {- $dimension-terms To facilitate parsing and pretty-printing functions that may wish to operate on term-level representations of dimension, we provide a means for converting from type-level dimensions to term-level dimensions. -} -- | Forms a new atomic 'Unit' by specifying its 'UnitName' and its definition as a multiple of another 'Unit'. -- -- Use this variant when the scale factor of the resulting unit is irrational or 'Approximate'. See 'mkUnitQ' for when it is rational -- and 'mkUnitZ' for when it is an integer. -- -- Note that supplying zero as a definining quantity is invalid, as the library relies -- upon units forming a group under multiplication. -- -- Supplying negative defining quantities is allowed and handled gracefully, but is discouraged -- on the grounds that it may be unexpected by other readers. mkUnitR :: Floating a => UnitName m -> ExactPi -> Unit m1 d a -> Unit m d a mkUnitR :: UnitName m -> ExactPi -> Unit m1 d a -> Unit m d a mkUnitR UnitName m n ExactPi s (Unit _ e _) | ExactPi -> Bool isExactZero ExactPi s = [Char] -> Unit m d a forall a. HasCallStack => [Char] -> a error [Char] "Supplying zero as a conversion factor is not valid." | Bool otherwise = UnitName m -> ExactPi -> a -> Unit m d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName m n ExactPi e' a x' where e' :: ExactPi e' = ExactPi s ExactPi -> ExactPi -> ExactPi forall a. Num a => a -> a -> a Prelude.* ExactPi e x' :: a x' = ExactPi -> a forall a. Floating a => ExactPi -> a approximateValue ExactPi e' -- | Forms a new atomic 'Unit' by specifying its 'UnitName' and its definition as a multiple of another 'Unit'. -- -- Use this variant when the scale factor of the resulting unit is rational. See 'mkUnitZ' for when it is an integer -- and 'mkUnitR' for the general case. -- -- For more information see 'mkUnitR'. mkUnitQ :: Fractional a => UnitName m -> Rational -> Unit m1 d a -> Unit m d a mkUnitQ :: UnitName m -> Rational -> Unit m1 d a -> Unit m d a mkUnitQ UnitName m n Rational s (Unit _ e x) | Rational s Rational -> Rational -> Bool forall a. Eq a => a -> a -> Bool == Rational 0 = [Char] -> Unit m d a forall a. HasCallStack => [Char] -> a error [Char] "Supplying zero as a conversion factor is not valid." | Just Rational x'' <- ExactPi -> Maybe Rational toExactRational ExactPi e' = UnitName m -> ExactPi -> a -> Unit m d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName m n ExactPi e' (Rational -> a forall a. Fractional a => Rational -> a fromRational Rational x'') | Bool otherwise = UnitName m -> ExactPi -> a -> Unit m d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName m n ExactPi e' a x' where e' :: ExactPi e' = Rational -> ExactPi forall a. Fractional a => Rational -> a fromRational Rational s ExactPi -> ExactPi -> ExactPi forall a. Num a => a -> a -> a Prelude.* ExactPi e x' :: a x' = Rational -> a forall a. Fractional a => Rational -> a fromRational Rational s a -> a -> a forall a. Num a => a -> a -> a Prelude.* a x -- | Forms a new atomic 'Unit' by specifying its 'UnitName' and its definition as a multiple of another 'Unit'. -- -- Use this variant when the scale factor of the resulting unit is an integer. See 'mkUnitQ' for when it is rational -- and 'mkUnitR' for the general case. -- -- For more information see 'mkUnitR'. mkUnitZ :: Num a => UnitName m -> Integer -> Unit m1 d a -> Unit m d a mkUnitZ :: UnitName m -> Integer -> Unit m1 d a -> Unit m d a mkUnitZ UnitName m n Integer s (Unit _ e x) | Integer s Integer -> Integer -> Bool forall a. Eq a => a -> a -> Bool == Integer 0 = [Char] -> Unit m d a forall a. HasCallStack => [Char] -> a error [Char] "Supplying zero as a conversion factor is not valid." | Just Integer x'' <- ExactPi -> Maybe Integer toExactInteger ExactPi e' = UnitName m -> ExactPi -> a -> Unit m d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName m n ExactPi e' (Integer -> a forall a. Num a => Integer -> a fromInteger Integer x'') | Bool otherwise = UnitName m -> ExactPi -> a -> Unit m d a forall (m :: Metricality) (d :: Dimension) a. UnitName m -> ExactPi -> a -> Dimensional ('DUnit m) d a Unit UnitName m n ExactPi e' a x' where e' :: ExactPi e' = Integer -> ExactPi forall a. Num a => Integer -> a fromInteger Integer s ExactPi -> ExactPi -> ExactPi forall a. Num a => a -> a -> a Prelude.* ExactPi e x' :: a x' = Integer -> a forall a. Num a => Integer -> a fromInteger Integer s a -> a -> a forall a. Num a => a -> a -> a Prelude.* a x