I was reading some interesting articles about simplifying programming:
- Out of the Tar Pit, on Reddit: https://redd.it/2ztw5o
- What, if Anything, Is a Declarative Language?, a blog article: https://is.gd/lBmORs
I’d like to add my own thoughts on “declarative” languages, which are often thought to be the Holy Grail of computing.
Declarative Languages are Walled Gardens. Wikipedia defines a walled garden as follows:
A closed platform, walled garden or closed ecosystem is a software system where the carrier or service provider has control over applications, content, and media, and restricts convenient access to non-approved applications or content. This is in contrast to an open platform, where consumers have unrestricted access to applications, content, and much more.
When I speak of walled gardens wrt declarative languages, I am not of course referring to vendor lock-in, but more in the way of “syntactic” lock-in. In other words, declarative languages do what the designers intended well, but present an impassible barrier when you want to do something the designers did not anticipate.
Take SQL, for example. SQL is a declarative language for querying databases. Simple queries are simple. But as the complexity of what you want to do increases, the queries become increasingly messy. You can’t assign results of queries to values, for example. So you can’t have intermediate query results. This means that all queries have to be done “in a oner”, potentially leading to some very hairy, convoluted code.
SQL doesn’t have state. So it’s difficult to do things like keeping a running balance – which is something pretty basic for an accounting package.
We already have the perfect tool for creating declarative languages: it’s called Lisp. If declarative languages were so great, why isn’t there an abundance of them in Lisp? Lisp macros allows you to extend the language in any way that you want. So it’s the perfect tool for declarative language construction. Except, we never seem to see big, stable, maintained, non-toy, declarative languages emerge from Lisp.
Maybe Lispers just aren’t interested in the same problems that the rest of us are. I dare say that many Lispers would state, rightly, that they are writing declarative languages. Technically this may be true, but I think they are languages “in the small”, designed to satisfy their own fairly esoteric (not meant as a pejorative) concerns. There’s been no big paradigm-busting declarative language to emerge from Lisp.
Wait, you don’t even need Lisp. Because really, every declarative language is, in essence, “just” an AST (abstract syntax tree). If you are too lazy to write your own parser, you can always hard-code your AST in the source language itself, and have it compiled in with the rest of your program. Lisp is not mandatory. It should be perfectly possible to do it in C, if a little messy. C++ would be nicer. Python might be even better, with Haskell at the pinnacle, perhaps. All you really need, though, is for the language to understand types and lists. In C++, for example, you could represent AST nodes as classes, and use vectors instead of lists.