[racket] Units/measures library

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Thu Nov 17 17:49:32 EST 2011

Shriram Krishnamurthi wrote at 11/17/2011 09:43 AM:
> DSSSL (-:
> (Since I believe Bigloo and/or Gambit implemented DSSSL, lurking in
> their implementations is code that does this...)

Also, the Curl programming language had units of measurement built-in:

> Quantities
> Presentation-oriented applications frequently deal with
> measurements of space and time. Curl accordingly includes
> “quantity types,” which are numeric data types with units, such as
> Distance and Time. These types are distinct from the
> conventional dimensionless numeric types such as float and
> double. There are also quantity literals, such as 2.54cm,
> 72pt, 50ms, 1week, and even 800dpi and 32(ft/(s^2)).
> By using quantities, programmers can let Curl handle conversions
> between units and can write more descriptive (and checkable)
> interfaces to graphical APIs. There is also a pixel-denominated
> unit px, which can be converted to a Distance by dividing by
> the spatial resolution of a display device. Quantity types are not
> found in other languages for creating Web content, but they are
> used pervasively in the Curl GUI APIs and are tremendously
> useful. in application programs.

It seems easy for someone to add to Racket as a library, so long as the 
library user doesn't mind having to explicitly ``cast'' these objects to 
normal Racket numbers when they want to interoperate with Racket 
libraries that only work with numbers. Maybe use Typed Racket or 
contracts info to add in the type coercions manually, or maybe having to 
do the explicit casts is actually good programming practice.

I am doing something like this with an (as yet unreleased) time library, 
and have thought of doing it for financial apps as well.

One thing I haven't decided is whether and how to represent precision 
(or resolution) information, and what to do with that information. For a 
simple example, what is the answer to the difference "(2 days) - (3 
minutes)"? I'm wondering whether the unit of measure alone can indicate 
the precision for 99.9% of practical purposes, or we have to be able to 
support more flexible specification of precision (e.g., "(2 days) - (3 
minutes to a precision of 15 seconds)").


Posted on the users mailing list.