|
In this article:
- What is Declarative Programming
- What are Domain Specific Languages
What is Declarative Programming?
Declarative programming bears two different meanings. These have been explained below:
- Declarative programming may refer to the method of programming where the program made defines ‘what’ something is and not ‘how’ to make it. This means that a program which is declarative does not tell anything about the implementation of a thing it describes (‘what’). The best example in this regard is that of HTML webpages. HTML webpages are only a collection of certain text, images or graphics and other elements and the HTML only defines what is to be displayed on a page and what it looks like. How it is implemented, that is, how it is actually shown is not its matter of concern. This is left to the browser. Thus, a declarative program is opposite to an imperative one such as those made in C or Java. These languages need the programmer to define the whole algorithm to achieve something.
- Declarative programming may also refer to the porgramming done in a functional, logic or
constraint programming language. “Declarative languages” is a term that includes all such languages.
The reason why these definitions are not completely distinct (that is, they overlap) is that most of the constraint and logic programming languages allow the programmer to define the algorithm of something to be done (that is, how). Another facet of this issue is that declarative programming can easily be done with imperative programming languages. To achieve this, all non-declarative parts of the program are put inside (or encapsulated) a library or framework. The rest of the program then becomes declarative. An example is that of the Junit test frameowork.
Under declarative programming, a data structure is written (that is, declared) by the programmer and there exists a standard algorithm with the help of which it is processed. In the example of HTML webpages, one only has to define what is to be displayed. The responsibility of understanding the code and executing it correctly (that is, displaying the elements) is left to the client (in this case, the browser).
There are certain features of declarative languages that are same as that of other languages. Firstly, a declarative language has a definite syntax what words are used to make a program. Secondly, its syntax also describes how the words (sometimes called keywords) may be grouped together. Finally, the syntax determines what sentences can be formed with the words and what they achieve.
What are Domain-specific languages?
Domain-specific languages or DSLs are the most popular form of declarative programming. The term ‘domain’ with regard to DSLs refers to the entity that the language defines. The most well-known domain-specific language is Hypertext Markup Language or HTML. Its domain is hypertext in general and webpages in particular. Spreadsheets (such as that in Microsoft Excel) and E-Mail are also examples of Domain-specific languages since they contain certain processing commands or instructions.
Domain-specific languages are often included (embedded) inside the programs of other programming languages. This is a feature rather than a limitation of DSLs. The limitation is that these languages are not Turing-complete and hence are not able to perform certain functions (as a Turing-complete language should do, it can perform all functions). For example, a spreadsheet cannot help us in sending an email. But this also has a certain advantage. Instead of programming in the DSL itself, a program may choose to use a more advanced language (say, Java) to build a complicated program and use a DSL only when one of its functions can be implemented well with a DSL. Thus, while difficult problems will be solved by the general-purpose language, the embedded DSL will take care of other simple problems.
It is notable that DSLs may also work indpendently and are then called Stand-alone DSLs and are not embedded into other languages.
Some examples of DSLs are XSLT, SQL (the part for relational databases) and TK Solver.
|