This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
handling of unit literals
- From: Per Bothner <per at bothner dot com>
- To: kawa at sourceware dot org
- Date: Wed, 14 Mar 2007 15:20:27 -0700
- Subject: handling of unit literals
Kawa supports "quantities" with units and dimensions -
see http://www.gnu.org/software/kawa/Quantities.html .
It also has a convenient syntax for literal quantities:
60s
12yard
Handling these is somewhat awakward: It the unit is one that
the Scheme reader knows about, the reader returns a literal value.
Otherwise, it assumes the units will be defined by the
time the "literal" is evaluated, and returns an expression instead:
(* 12 yard$unit)
I think it would be better to consistently defer "unit resolution"
to "name resolution time". A token that the lexer currently reads
as a quantity it would instead read as a symbol. Then at name
solution time, Kawa would look for a matching symbol in the
lexical scope or the dynamic environment. It there is no
match, it would look for a matching unit declaration.
For example:
(define-unit yard (* 12 3 2.54in))
This might viewed as syntactic sugar for:
(define-constant units:yard (* 12 3 2.54in))
Then the expression:
9yard
Assuming there is no lexical or compile-time-binding for the symbol
'9yard, Kawa would look for a binding for units:yard. Finding this,
it would transform (early at compile-time) the expression to:
(* 9 units:yard)
If there is binding for units:yard we get the usual run-time error
(and/or compile-time warning if the option --warn-undefined-variable
was specified).
An advantage is better compatibility with other Scheme implementations.
In particular, '12cm would evaluate to a symbol, not a quantity.
A bigger advantage is that unit names are handled similar to
other names: They could be defined in a local scope, exported
from a module, etc.
A disadvantage is there would no longer be "quantity literals",
so for example this would no longer work:
#(2.54cm 1cm 0.5cm 5cm)
You'd have to write:
(vector 2.54cm 1cm 0.5cm 5cm)
--
--Per Bothner
per@bothner.com http://per.bothner.com/