views:

145

answers:

3

I've decided to build a database tracking the credit card offers I get in the mail, and one confounding factor has been how to represent the offers. With minimum finance charges and variable rates and mimimum variable rates it gets a bit more complex than "10.99 percent". I've decided I need to construct a language to represent these things if I have a shot at comparing or trending these offers.

The question is, how do I represent this? The formulas are far too complex to model by creating fields, so I'll just store the formula in the DB in all it's glory. Yes, this means blurring the line between data and execution, but since this is a personal project I'm not so worried.

  1. Embedded python. Represent the formula as Python and just bind relevant variables and call the formula.
  2. Use a Domain Specific Language. I can't be the first person to think of representing things this way, and in fact I know I'm not. Are there any free, public languages that can represent this?

Can anyone offer advice on which approach is better?

+1  A: 

My vote is for Embedded Python, if you feel you must store the formulas in the database.

A DSL seems like overkill; you already have a perfectly good language for evaluating formulas.

Robert Harvey
+2  A: 

This sounds somewhat similar to the work by Simon Peyton Jones et al. on representing financial contracts using combinator libraries, including an implementation in Haskell, including the ability to run evaluation functions over them to compute the value of the contract given some model. I don't know enough about finance to tell you if this is exactly what you're looking for, but it sounds like it's in the right ballpark.

As to your specific question, I generally prefer embedded domain specific languages (that is, DSL-like constructs embedded in a host language, such as Ruby with its object oriented metaprogramming, Scheme with its macros, or Haskell combinator libraries), since it give you the full power of the host language for free; usually when writing a domain specific language with its own syntax, execution model, runtime, and so on, you run into things that would be easy to do if you had an existing language available, but are a pain to deal with since you haven't implemented something in your DSL.

Brian Campbell
The advantage, I think, may be it's easier to manipulate parse trees of your DSL than borrowed grammar.
jldugger
There are advantages to both approaches. I find it's better to start with an embedded DSL, as you're less likely to spend lots of time up front re-implementing something that's already been implemented better in your host language for no reason; then if it turns out that a better syntax really will help you express your ideas (which it can in some cases), you can write your parser and have it use everything you built for your embedded DSL on the back end. Likewise, when I'm designing macros in Scheme, I try to design a good regular API first, and just use the macro as a thin layer over thet.
Brian Campbell
+1  A: 

Another option is to use R. It has a nice formula representation; you store it as a string and then evaluate it with the eval() function. There are also extensive financial libraries (see the CRAN finance view), including Rmetrics.

R also interacts very nicely with other languages, including python, java, and C++. You mentioned Python: you can use RPy if you want to write a wrapper script.

Shane