Measure (empty) → 0.0.1
raw patch · 8 files changed
+403/−0 lines, 8 filesdep +basesetup-changed
Dependencies added: base
Files
- Data/Measure/Angle.hs +104/−0
- Data/Measure/ConvertDouble.hs +8/−0
- Data/Measure/Length.hs +219/−0
- Data/Measure/RelativeDouble.hs +7/−0
- LICENSE +27/−0
- Measure.cabal +28/−0
- README +7/−0
- Setup.hs +3/−0
+ Data/Measure/Angle.hs view
@@ -0,0 +1,104 @@+{-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses #-}++-- | For measuring the vertex of angles.+--+-- @+-- Data.Measure.Angle\> 127 ..> degrees <.. radians+-- 2.2165681500327987+-- @+--+-- @+-- Data.Measure.Angle\> 2478 ..> mils <.. grads+-- 154.875+-- @+--+-- @+-- Data.Measure.Angle\> 24 ..> arcminutes <.. sextants+-- 6.666666666666666e-3+-- @+module Data.Measure.Angle(AngleMeasure,+ rad,+ radians,+ degrees,+ mils,+ arcminutes,+ arcseconds,+ grads,+ octants,+ quadrants,+ sextants,+ signs,+ Angle,+ rad',+ angle) where++import Data.Measure.RelativeDouble+import Data.Measure.ConvertDouble++-- | A ratio of measurement of an angle.+newtype AngleMeasure = Radian {+ rad :: Double -- ^ The ratio of measurement of an angle in radians.+} deriving (Eq, Ord, Enum, Num, Fractional, Floating)++instance Show AngleMeasure where+ show = show . rad++-- | A measurement of angles.+radians :: AngleMeasure+radians = Radian 1++-- | A measurement of angles in degrees.+degrees :: AngleMeasure+degrees = radians .*. pi / 180++-- | A measurement of angular mils.+mils :: AngleMeasure+mils = radians .*. pi / 3200++-- | A measurement of arcminutes.+arcminutes :: AngleMeasure+arcminutes = degrees ./. 60++-- | A measurement of arcseconds.+arcseconds :: AngleMeasure+arcseconds = arcminutes ./. 60++-- | A measurement of grads.+grads :: AngleMeasure+grads = radians .*. pi / 200++-- | A measurement of octants.+octants :: AngleMeasure+octants = degrees .*. 45++-- | A measurement of quadrants.+quadrants :: AngleMeasure+quadrants = octants .*. 2++-- | A measurement of sextants.+sextants :: AngleMeasure+sextants = degrees .*. 60++-- | A measurement of signs.+signs :: AngleMeasure+signs = degrees .*. 30++-- | A measurement of an angle in radians.+newtype Angle = Angle {+ rad' :: Double -- ^ The measurement of an angle in radians.+} deriving (Eq, Ord, Enum, Num, Fractional, Floating)++-- | Construct an @Angle@ from a floating-point value.+angle :: Double -> Angle+angle = Angle++instance Show Angle where+ show (Angle d) = show d ++ "rad"++instance RelativeDouble AngleMeasure where+ Radian n .*. r = Radian (n * r)+ Radian n ./. r = Radian (n / r)++instance ConvertDouble AngleMeasure Angle where+ x ..> n = Angle (x * rad n)+ n <.. x = rad' n / rad x
+ Data/Measure/ConvertDouble.hs view
@@ -0,0 +1,8 @@+{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}++-- | A measurement that is represented by a floating-point value that can be converted by differing units.+module Data.Measure.ConvertDouble where++class ConvertDouble u v | u -> v where+ (..>) :: Double -> u -> v -- ^ Convert to a unit of measurement.+ (<..) :: v -> u -> Double -- ^ Convert from a unit of measurement.
+ Data/Measure/Length.hs view
@@ -0,0 +1,219 @@+{-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses #-}++-- | For measuring length, the distance between two points.+--+-- @+-- Data.Measure.Length\> 27 ..> miles <.. metres+-- 43452.288+-- @+--+-- @+-- Data.Measure.Length\> 14.6 ..> kilometres <.. nauticalMiles+-- 7.883369330453564+-- @+--+-- @+-- Data.Measure.Length\> 14.6 ..> parsecs <.. kilometres+-- 4.5050896172e14+-- @+module Data.Measure.Length(LengthMeasure,+ nm,+ yoctometres,+ zeptometres,+ attometres,+ femtometres,+ fermis,+ picometres,+ nanometres,+ micrometres,+ millimetres,+ centimetres,+ decimetres,+ metres,+ kilometres,+ au,+ lightSeconds,+ lightMinutes,+ lightHours,+ lightDays,+ lightYears,+ parsecs,+ ells,+ inches,+ thou,+ feet,+ yards,+ quarters,+ miles,+ nauticalMiles,+ nauticalMilesAdmiralty,+ fathoms,+ cables,+ chains,+ furlongs,+ Length,+ nm',+ length') where++import Data.Measure.RelativeDouble+import Data.Measure.ConvertDouble++-- | A ratio of measurement of length to nanometres.+newtype LengthMeasure = Nanometre {+ nm :: Double -- ^ The ratio of measurement of length in nanometres.+} deriving (Eq, Ord, Enum, Num, Fractional, Floating)++instance Show LengthMeasure where+ show = show . nm++-- | A measurement of length, 10 ^ -24 metres.+yoctometres :: LengthMeasure+yoctometres = zeptometres ./. 1000++-- | A measurement of length, 10 ^ -21 metres.+zeptometres :: LengthMeasure+zeptometres = attometres ./. 1000++-- | A measurement of length, 10 ^ -18 metres.+attometres :: LengthMeasure+attometres = femtometres ./. 1000++-- | A measurement of length, 10 ^ -15 metres.+femtometres :: LengthMeasure+femtometres = picometres ./. 1000++-- | A measurement of length, 10 ^ -15 metres.+fermis :: LengthMeasure+fermis = femtometres++-- | A measurement of length, 10 ^ -12 metres.+picometres :: LengthMeasure+picometres = nanometres ./. 1000++-- | A measurement of length, 10 ^ -9 metres.+nanometres :: LengthMeasure+nanometres = Nanometre 1++-- | A measurement of length, 10 ^ -6 metres.+micrometres :: LengthMeasure+micrometres = nanometres .*. 1000++-- | A measurement of length, 10 ^ -3 metres.+millimetres :: LengthMeasure+millimetres = micrometres .*. 1000++-- | A measurement of length, 10 ^ -2 metres.+centimetres :: LengthMeasure+centimetres = millimetres .*. 10++-- | A measurement of length, 10 ^ -1 metres.+decimetres :: LengthMeasure+decimetres = centimetres .*. 10++-- | A measurement of length.+metres :: LengthMeasure+metres = millimetres .*. 1000++-- | A measurement of length, 10 ^ 3 metres.+kilometres :: LengthMeasure+kilometres = metres .*. 1000++-- | A measurement of length, Astronomical Units.+au :: LengthMeasure+au = metres .*. 149597871464++-- | A measurement of length, the distance light travels in a vaccuum in one second.+lightSeconds :: LengthMeasure+lightSeconds = metres .*. 299792458++-- | A measurement of length, the distance light travels in a vaccuum in one minute.+lightMinutes :: LengthMeasure+lightMinutes = lightSeconds .*. 60++-- | A measurement of length, the distance light travels in a vaccuum in one hour.+lightHours :: LengthMeasure+lightHours = lightMinutes .*. 60++-- | A measurement of length, the distance light travels in a vaccuum in one day.+lightDays :: LengthMeasure+lightDays = lightHours .*. 24++-- | A measurement of length, the distance light travels in a vaccuum in one (Julian) year.+lightYears :: LengthMeasure+lightYears = metres .*. 9460730472580800++-- | A measurement of length, parallax of one arcsecond.+parsecs :: LengthMeasure+parsecs = kilometres .*. 30856778200000++-- | A measurement of length, approximating the length of a man's arm.+ells :: LengthMeasure+ells = metres .*. 1.143++-- | A measurement of length, 25.4 millimetres.+inches :: LengthMeasure+inches = millimetres .*. 25.4++-- | A measurement of length, 0.0254 millimetres.+thou :: LengthMeasure+thou = inches ./. 1000++-- | A measurement of length, 12 inches.+feet :: LengthMeasure+feet = inches .*. 12++-- | A measurement of length, 3 feet.+yards :: LengthMeasure+yards = feet .*. 3++-- | A measurement of length, a quarter of a yard.+quarters :: LengthMeasure+quarters = yards ./. 4++-- | A measurement of length, 1760 yards.+miles :: LengthMeasure+miles = yards .*. 1760++-- | A measurement of length, 1852 metres.+nauticalMiles :: LengthMeasure+nauticalMiles = metres .*. 1852++-- | A measurement of length, 6080 feet.+nauticalMilesAdmiralty :: LengthMeasure+nauticalMilesAdmiralty = feet .*. 6080++-- | A measurement of length, 1828.8 millimetres.+fathoms :: LengthMeasure+fathoms = micrometres .*. 1828800++-- | A measurement of length, 100 fathoms.+cables :: LengthMeasure+cables = fathoms .*. 100++-- | A measurement of length, 66 feet.+chains :: LengthMeasure+chains = feet .*. 66++-- | A measurement of length, 10 chains.+furlongs :: LengthMeasure+furlongs = chains .*. 10++-- | A measurement of length in nanometres.+newtype Length = Length {+ nm' :: Double -- ^ The number of nanometres.+} deriving (Eq, Ord, Enum, Num, Fractional, Floating)++-- | Construct a @Length@ from a floating-point value.+length' :: Double -> Length+length' = Length++instance Show Length where+ show (Length d) = show d ++ "nm"++instance RelativeDouble LengthMeasure where+ Nanometre n .*. r = Nanometre (n * r)+ Nanometre n ./. r = Nanometre (n / r)++instance ConvertDouble LengthMeasure Length where+ x ..> n = Length (x * nm n)+ n <.. x = nm' n / nm x
+ Data/Measure/RelativeDouble.hs view
@@ -0,0 +1,7 @@+-- | A measurement that is relative to another by a floating-point value.+module Data.Measure.RelativeDouble where++-- | A class of data types that are a measurement relative by a floating-point value.+class RelativeDouble t where+ (.*.) :: t -> Double -> t -- ^ Relative by a product.+ (./.) :: t -> Double -> t -- ^ Relative by a quotient.
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright 2009 Tony Morris++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:+1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+3. Neither the name of the author nor the names of his contributors+ may be used to endorse or promote products derived from this software+ without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF+SUCH DAMAGE.
+ Measure.cabal view
@@ -0,0 +1,28 @@+Name: Measure+Version: 0.0.1+License: BSD3+License-File: LICENSE+Author: Tony Morris <code@tmorris.net>+Maintainer: Tony Morris+Synopsis: A library for units of measurement+Category: Development+Description: A library for units of measurement+Cabal-version: >= 1.2+Build-Type: Simple+Extra-Source-Files: README++Flag small_base+ Description: Choose the new, split-up base package.++Library+ if flag(small_base)+ Build-Depends: base < 4 && >= 3+ else+ Build-Depends: base < 3++ GHC-Options: -Wall+ Exposed-Modules:+ Data.Measure.Length,+ Data.Measure.Angle,+ Data.Measure.RelativeDouble,+ Data.Measure.ConvertDouble
+ README view
@@ -0,0 +1,7 @@+Measure+=======++A library for units of measurement. Currently only measurement of length and angles are supported. Suggestions are welcome regarding the design of the library, particularly the type-classes and use of types. See the haddock for usage examples.++Tony Morris+code@tmorris.net
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple+main = defaultMain+