Personal tools

Physical units

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(CalDims)
(new package unittyped)
(6 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
* It can. Look at [[Dimensionalized numbers]] for a toy implementation. I've thought a bit about the Geometric Algebra case, but I can't see a good way of handling it without forcing a choice of basis. I'm also not sure how it would work -- the whole point of GA is to incorporate areas, lengths, volumes, etc. into one number type. I suppose we could use this type of technique to segregate GA spaces with different dimensions or even metrics. --AaronDenney
 
* It can. Look at [[Dimensionalized numbers]] for a toy implementation. I've thought a bit about the Geometric Algebra case, but I can't see a good way of handling it without forcing a choice of basis. I'm also not sure how it would work -- the whole point of GA is to incorporate areas, lengths, volumes, etc. into one number type. I suppose we could use this type of technique to segregate GA spaces with different dimensions or even metrics. --AaronDenney
   
* NumericPrelude also contains an implementation of values equipped with physical units. However all unit checking is made dynamically, that is expressions like <hask>1 * meter < 2 * second</hask> are accepted by the Haskell compiler but lead to a runtime error. This design allows multiplication of values with different units, but prohibits catching unit errors at compile time. Units are handled as integer vectors of exponents of base units. Computation with these vectors is rather simple, however formatting them is difficult. Formatting and parsing values with units is supported. A small library of SI units is provided.
+
* A (non-toy) implementation of statically checked physical dimensions with a complete set of dimensions (but limited number of predefined units) is [http://code.google.com/p/dimensional/ Dimensional]. I apologize for coming up with the same name as Aaron and am on the lookout for a new one. I cannot comment on the applicability to GA. --[[User:Bjorn|Bjorn]]
: http://darcs.haskell.org/numericprelude/src/Number/Physical.hs
 
: http://darcs.haskell.org/numericprelude/src/Number/SI.hs
 
   
:Get with <code>darcs get http://darcs.haskell.org/numericprelude/</code>
+
* NumericPrelude also contains an implementation of values equipped with physical units. However all unit checking is made dynamically, that is expressions like <hask>1 * meter < 2 * second</hask> are accepted by the Haskell compiler but lead to a runtime error. This design allows the programmer to process values from IO world with units, that are unknown at compile time, but it prohibits catching unit errors at compile time. Units are handled as integer vectors of exponents of base units. Computation with these vectors is rather simple, however formatting them is difficult. Formatting and parsing values with units is supported. A small library of SI units is provided.
  +
: http://code.haskell.org/numeric-prelude/src/Number/Physical.hs
  +
: http://code.haskell.org/numeric-prelude/src/Number/SI.hs
  +
  +
:Get with <code>darcs get http://code.haskell.org/numeric-prelude/</code>
  +
  +
* poor man's type level [http://code.haskell.org/numeric-prelude/src/Number/DimensionTerm.hs physical dimensions] in [[Numeric Prelude]]
   
 
* [[CalDims]]
 
* [[CalDims]]
  +
  +
* {{HackagePackage|id=unittyped}}
   
   

Revision as of 11:04, 6 December 2012

How would one go about modeling units (seconds, meters, meters per second, etc) in Haskell? I'm particularly interested in getting the typechecker to verify proper usage, and do not want to restrict it to any particular numeric representation (i.e. both integral seconds and fractional seconds). If this can in fact be done, it could also be used to model coordinate system axes in, say, Geometric Algebra.

  • It can. Look at Dimensionalized numbers for a toy implementation. I've thought a bit about the Geometric Algebra case, but I can't see a good way of handling it without forcing a choice of basis. I'm also not sure how it would work -- the whole point of GA is to incorporate areas, lengths, volumes, etc. into one number type. I suppose we could use this type of technique to segregate GA spaces with different dimensions or even metrics. --AaronDenney
  • A (non-toy) implementation of statically checked physical dimensions with a complete set of dimensions (but limited number of predefined units) is Dimensional. I apologize for coming up with the same name as Aaron and am on the lookout for a new one. I cannot comment on the applicability to GA. --Bjorn
  • NumericPrelude also contains an implementation of values equipped with physical units. However all unit checking is made dynamically, that is expressions like
    1 * meter < 2 * second
    are accepted by the Haskell compiler but lead to a runtime error. This design allows the programmer to process values from IO world with units, that are unknown at compile time, but it prohibits catching unit errors at compile time. Units are handled as integer vectors of exponents of base units. Computation with these vectors is rather simple, however formatting them is difficult. Formatting and parsing values with units is supported. A small library of SI units is provided.
http://code.haskell.org/numeric-prelude/src/Number/Physical.hs
http://code.haskell.org/numeric-prelude/src/Number/SI.hs
Get with darcs get http://code.haskell.org/numeric-prelude/