Request for Comments - hscurrency 0.0.1

Hi all, I'm putting together some simple tools to do safe calculations on different currencies. For instance, making sure that you dont add something like 5 USD + 10 JPY without doing a proper conversion. I've put up some code on google code which probably explains what I'm trying to do more succinctly than this email. I'm curious what poeple think about the library, its the first haskell code I've written for the purpose of sharing and I intend to add it to hackage once I finalize the interface a bit more. The code is at: http://bit.ly/1Cjjlj I'm very open to suggestions on improving the interface. RIght now its very simple and straightforward but potentially limited. I originally wrote it so that the Prc type would be an instance of several numerical classes but eventually stripped that out as it required returning undefined in several cases (such as calling toIntegral on a Prc which was a composite of several currencies). I felt it was better to avoid having partial functions.. Max

On Sat, Aug 15, 2009 at 7:15 AM, Max Cantor
Hi all,
I'm putting together some simple tools to do safe calculations on different currencies. For instance, making sure that you dont add something like 5 USD + 10 JPY without doing a proper conversion.
I've put up some code on google code which probably explains what I'm trying to do more succinctly than this email. I'm curious what poeple think about the library, its the first haskell code I've written for the purpose of sharing and I intend to add it to hackage once I finalize the interface a bit more.
The code is at: http://bit.ly/1Cjjlj
I'm very open to suggestions on improving the interface. RIght now its very simple and straightforward but potentially limited.
I originally wrote it so that the Prc type would be an instance of several numerical classes but eventually stripped that out as it required returning undefined in several cases (such as calling toIntegral on a Prc which was a composite of several currencies). I felt it was better to avoid having partial functions..
The names aren't immediately clear to me, but maybe they make sense in the discipline of currency trading. Are these sorts of calculations commonly done with double precision floats? Doing any sort of currency math with binary floating point numbers feels odd to me, but again, I may not be the target for this sort of library. Antoine

On Sat, Aug 15, 2009 at 5:15 AM, Max Cantor
Hi all,
I'm putting together some simple tools to do safe calculations on different currencies. For instance, making sure that you dont add something like 5 USD + 10 JPY without doing a proper conversion.
I've put up some code on google code which probably explains what I'm trying to do more succinctly than this email. I'm curious what poeple think about the library, its the first haskell code I've written for the purpose of sharing and I intend to add it to hackage once I finalize the interface a bit more.
The code is at: http://bit.ly/1Cjjlj
I'm very open to suggestions on improving the interface. RIght now its very simple and straightforward but potentially limited.
Right now it looks like you have taken the approach of embedded domain specific language. You have not exposed the currency units to the type system. Therefore you rely on the rules of your embedded language to do proper conversions. Have you considered exposing the type information to the type checker? A similar question came up recently on a Portland functional programming mailing list and my reply can be found here: http://groups.google.com/group/pdxfunc/tree/browse_frm/month/2009-08/5c565768ecf30c57?rnum=1&_done=%2Fgroup%2Fpdxfunc%2Fbrowse_frm%2Fmonth%2F2009-08%3F#doc_5c565768ecf30c57 The experimental code which resulted is here: http://gist.github.com/165691 You may also want to look at the dimensional package: http://code.google.com/p/dimensional/ Jason

@Jason I'm not sure what you mean about exposing the type information. Unless you mean that each currency would be a separate type somehow. While this is a similar problem to the dimensional issue, the problem is that the FX rates are changing all the time. While the conversion between a meter and a foot is always constant, with FX rates thats not the case. So 2ft + 3m is always a well defined value but something like USD 1 + JPY 1 gives a function of the USDJPY exchange rate, not a constant value. @ Antoine I'll add some comments. you're right that doubles are not typically used nor would they be in a finished product. for the time being, however, I dont know if its better to use Ratio's, Fixed's or what, so just settled on the most straightforward for now. On Aug 16, 2009, at 1:26 AM, Jason Dagit wrote:
On Sat, Aug 15, 2009 at 5:15 AM, Max Cantor
wrote: Hi all, I'm putting together some simple tools to do safe calculations on different currencies. For instance, making sure that you dont add something like 5 USD + 10 JPY without doing a proper conversion.
I've put up some code on google code which probably explains what I'm trying to do more succinctly than this email. I'm curious what poeple think about the library, its the first haskell code I've written for the purpose of sharing and I intend to add it to hackage once I finalize the interface a bit more.
The code is at: http://bit.ly/1Cjjlj
I'm very open to suggestions on improving the interface. RIght now its very simple and straightforward but potentially limited.
Right now it looks like you have taken the approach of embedded domain specific language. You have not exposed the currency units to the type system. Therefore you rely on the rules of your embedded language to do proper conversions.
Have you considered exposing the type information to the type checker? A similar question came up recently on a Portland functional programming mailing list and my reply can be found here: http://groups.google.com/group/pdxfunc/tree/browse_frm/month/2009-08/5c565768ecf30c57?rnum=1&_done=%2Fgroup%2Fpdxfunc%2Fbrowse_frm%2Fmonth%2F2009-08%3F#doc_5c565768ecf30c57
The experimental code which resulted is here: http://gist.github.com/165691
You may also want to look at the dimensional package: http://code.google.com/p/dimensional/
Jason

Hello,
To let the type checker do some work for you, without getting all the
way into the territory of the dimensional package, you can use
newtypes and a Units class with methods for wrapping and unwrapping
Doubles; we use this approach at work and find it strikes a nice
balance between useful (static typechecking) and usable (unobtrusive
to introduce to existing codebase). Just as in your code, conversions
are written explicitly by us and can change when needed (with a
recompile), or can require an IO action to make use of changing data.
-Jeff
On Sun, Aug 16, 2009 at 9:55 AM, Max Cantor
@Jason I'm not sure what you mean about exposing the type information. Unless you mean that each currency would be a separate type somehow. While this is a similar problem to the dimensional issue, the problem is that the FX rates are changing all the time. While the conversion between a meter and a foot is always constant, with FX rates thats not the case. So 2ft + 3m is always a well defined value but something like USD 1 + JPY 1 gives a function of the USDJPY exchange rate, not a constant value.
@ Antoine I'll add some comments. you're right that doubles are not typically used nor would they be in a finished product. for the time being, however, I dont know if its better to use Ratio's, Fixed's or what, so just settled on the most straightforward for now.
On Aug 16, 2009, at 1:26 AM, Jason Dagit wrote:
On Sat, Aug 15, 2009 at 5:15 AM, Max Cantor
wrote: Hi all, I'm putting together some simple tools to do safe calculations on different currencies. For instance, making sure that you dont add something like 5 USD + 10 JPY without doing a proper conversion.
I've put up some code on google code which probably explains what I'm trying to do more succinctly than this email. I'm curious what poeple think about the library, its the first haskell code I've written for the purpose of sharing and I intend to add it to hackage once I finalize the interface a bit more.
The code is at: http://bit.ly/1Cjjlj
I'm very open to suggestions on improving the interface. RIght now its very simple and straightforward but potentially limited.
Right now it looks like you have taken the approach of embedded domain specific language. You have not exposed the currency units to the type system. Therefore you rely on the rules of your embedded language to do proper conversions.
Have you considered exposing the type information to the type checker? A similar question came up recently on a Portland functional programming mailing list and my reply can be found here:
The experimental code which resulted is here: http://gist.github.com/165691
You may also want to look at the dimensional package: http://code.google.com/p/dimensional/
Jason
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks for sharing this. If you haven't already, also check out http://hledger.org/api-doc -> Amount and Commodity modules for possibly related work.
participants (5)
-
Antoine Latter
-
Jason Dagit
-
jeff p
-
Max Cantor
-
Simon Michael