In scientific computing, units give a valuable check to make sure that all calculations were valid. For example, adding meters to seconds is a nonsensical operation, and should thus be reported as an error. Mathematica allows for unit checking with its Units package. However, few general purpose programming provide this functionality. In some languages, such as python, it is possible to add unit-checking arithmetic operations to objects. Ideally, an invalid computation will be caught at compile-time in the type system. This guarantees that there are no unit errors in a specific piece of code.
Haskell is a purely functional, lazy programming language that arose in the late 80s as a unification of the many lazy languages that had arisen up until that point. One of Haskell’s compelling features is its type system. A unit checker can be built on top of the Haskell language without any modifications to the language itself.
This article [1] describes a how to create such a unit checker. It uses the Peano numerals to perform integer arithmetic in the type system, which is then used to implement the unit checker. The unit checker itself can be downloaded as a Literate Haskell File. In addition to unit checking, this library provides automatic unit conversion. Examples are:
> gravity = kilo gram 9.8 .* meter 1 ./ (second 1 .* second 1)
> speedOfLight = meter (3 * 10^8) ./ second 1
> distance = meter 30 + kilo meter 2
This kind of unit checking can prove invaluable in numerical scientific applications. On September 23 1999, a Mars Climate Orbiter was mis-steered within 60 km of the planet’s surface, and was destroyed [2]. The mishap occurred because the software of one of the spacecraft’s components used imperial units, in stead of SI units, like the rest of the craft. If a unit checking system, such as the one described in this post had been available to the engineers of the spacecraft, this fatal mistake would have been prevented.
References:
[1] http://liftm.wordpress.com (by Samuel Hoffstaetter)
[2] http://findarticles.com/p/articles/mi_m1590/is_5_56/ai_57534967






Leave a Comment
You must be logged in to post a comment.
* You can follow any responses to this entry through the RSS 2.0 feed.