[Haskell-cafe] Announce: Decimal arithmetic library (questions inside)

Jeremy Shaw jeremy.shaw at linspireinc.com
Mon Oct 17 18:35:14 EDT 2005


Announcment:
------------

I am proud to announce the *premature* release of my Decimal
arithmetic library:

darcs get http://www.n-heptane.com/nhlab/repos/Decimal

It implements real decimal arithmetic, for cases where the binary
floating point is not acceptable (money, etc).

Need Advice:
-----------

I am currently stuck trying to figure out how to implement contexts.
The decimal operations are supposed to be executed in a context that
determines things such as, type of rounding, how many digits of
precision, what signals should be considered exceptions, etc. More
details at the end of this page:

http://www2.hursley.ibm.com/decimal/damodel.html

Ideally, the user should be able to have multiple contexts in the same
program.

So far the best solution I have seen is the solution presented in
'Functional Pearl: Implicit Configurations' to deal with modular
arithmetic.

http://www.eecs.harvard.edu/~ccshan/prepose/prepose.pdf

However, this solution seems a bit over the top -- I would like to
implement things such that the finished product could be included in
fptools.

One option would be to define a unique type for each context. So by
default I could provide DecimalFloat, and DecimalDouble. If the user
wants a different context then they create a new type. This is a bit
annoying however, because the user will have to create a bunch of
boilerplate code to add the new type to all the right classes. I could
probably automate that with TH however...

Another option might be to include the context in each Decimal
value. However, to check for context mismatches at compile time will
probably require encoding the context into a phantom type, or
something of that nature...

I have not done a lot of numeric programming, so I am don't really
know what people are even expecting or used to.

Background:
----------

I plan to use this library as the basis of a Money library. The python
folks have done a fine job of decribing why I am doing this which you
can read here:

 http://www.python.org/peps/pep-0327.html

I have been working from this spec so far:

 http://www2.hursley.ibm.com/decimal/decarith.html


What's Done So Far:
------------------

I have created instances for Decimal for the type classes:

 ~ Eq
 ~ Ord
 ~ Num (precision/rounding not enforced correctly)
 ~ Fractional

I am also going to investigate adding instances for:

 ~ Real
 ~ RealFrac
 ~ RealFloat
 ~ Floating

The Future:
-----------

There is still a fair bit of work to do long term:

 ~ Handle contexts
 ~ Enforce precision limits
 ~ Finish test suite
 ~ Add additional class instances
 ~ Optimize for speed

But, I want to get the context stuff figured out first, so I don't
have to rewrite everything later.

Thanks!
Jeremy Shaw.


More information about the Haskell-Cafe mailing list