Programming paradigms, ways of thinking about and structuring code has been around since the dawn of time – since man started to think of the goto statement as a bad thing and structured programming made its way into the spotlight. The major paradigms are
Of the first two, examples are everywhere. Imperative languages are everywhere. Examples are almost all popular languages: C, C++, C#, Java, Python, Perl, Ruby, the list goes on and on. Functional languages are less common, though increasing in popularity since they offer good ways of handling concurrency and parallellism. Examples are Haskell, F#, Clojure and LISP.
But what of the third? Can you name a declarative language?
When you program in a declarative style, you tell the computer what you want instead of telling it what to do. There are very few declarative languages, though one has reached a ubiquitous status, SQL. Think about it, apart from T-SQL, the bastard lovechild of imperative and declarative database access, SQL will never require you to tell it how to get its data, you tell it the rules that the data you want back will obey.
What if you could do the same sort of problem solving in your code?
As an example, think about how to go about implementing business rules. If there is one thing that software development has taught me it’s that the people who dream up rules for calculating prices have no end to their imagination. Say that you have built a system for shipping. There’s a fixed cost onto which a variable cost per mile is added to give the total shipping cost.
Fair enough, given the information you make very simple function to get the price. Then, as things are bound to do, a new rule enters. If you ship over a certain distance, you pay only 75% of the fixed cost. Fine, a simple if-statement will fix that.
Then the next change request. If you ship to a certain destination the variable price is lower. And if you ship more than 10 items you get a discount. If you book the shipping early it’s cheaper, if you book it late it’s more expensive.
Can you see where this is headed? Because of the permutation effects, every new case you add to this sort of computations will have to work together with every other case you have in your code, and still produce correct results.
This is a case where logic directed programming should shine. And there exists to my knowledge only one logic based language, Prolog.
I’ve been meaning to explore this further, but it appears that Prolog is about as out of style as you can get. There seems to have been some hype during the fifth generation computer project in the eighties but since that failed horribly it seems to have taken down Prolog with it.
Also, Prolog is a decidedly comp-sciency language and quite mathematical where as far as I can tell the applications of the paradigms are in the realm of the very practical. This gap doesn’t exactly help.
Is there anyone using it? Or is there another way to express rule logic based programming in a declarative style? It would be sort of cool to have a generic rule based engine available since these sorts of problems are so common.