Category: 

What is Declarative Programming?

Article Details
  • Written By: Archana Khambekar
  • Edited By: C. Wilborn
  • Last Modified Date: 28 August 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The atmosphere of Jupiter's moon Io collapses every time it is eclipsed by the planet.   more...

September 29 ,  2008 :  The Dow Jones Industrial Average experienced its largest one-day drop in history.  more...

Declarative programming is a way of specifying what a program should do, rather than specifying how to do it. Most computer languages are based on the steps needed to solve a problem, but some languages only indicate the essential characteristics of the problem and leave it to the computer to determine the best way to solve the problem. The former languages are said to support imperative programming whereas the latter support declarative programming.

One can consider the following example from SQL to get all the sales regions where profit exceeds a certain number, say, $700 US Dollars (USD), from the sales data.

Select Region, Profit from Sales where Profit > 700

This statement does not indicate what the SQL system has to do in order to get the data. The SQL system can go through each sales record and determine whether the condition is satisfied, or, it can quickly obtain the top few records from presorted data. The statement only indicates the nature of the desired data.

Prolog is a declarative programming language which indicates the logical relations between entities.

Ad

ancestor(M, C) :- mother(M, C)
ancestor(X, Z) :- mother(X, Y), ancestor(Y, Z)

The above program indicates truisms. If M is the mother of C then M is an ancestor of C. If X is the mother of some person Y who is an ancestor of Z, then X is an ancestor of Z. Given this and some mother-child facts, the program can answer questions about the whole maternal family tree.

During program execution, the Prolog system builds up a number of true statements, thus creating a knowledgebase. This knowledgebase is searched efficiently on its own. The creator of the above program does not specify how to search.

HTML and CSS are declarative programming languages. For instance, the HTML example <table border="1">, indicates a thin border. A CSS example is color: blue. This specifies the text color. As can be seen in these examples, HTML and CSS specify what should appear on a web page but not how to do so.

The advantage of declarative programming languages is mainly two-fold. The programs are concise; this makes it easy even for non-programmers to obtain solutions. In the SQL example above, an analyst or business support person can get the desired information. Similarly, laypersons can write acceptable web pages with simple HTML and CSS commands.

The second advantage of the declarative programming model is that repetitive imperative code that indicates how to solve things is provided in the computer system behind the scenes. Such code can be made highly efficient and can incorporate the best ideas from computing. It can take advantage of parallelism.

Ad

You might also Like

Recommended

Discuss this Article

nony
Post 2

@NathanG - There’s a guy I work with who’s really excited about Ruby programming because of its declarative nature. He says it’s more concise than traditional programming languages. I’ve seen some of the code, and it just looks like a different language.

I’m not sure I understand what the hype is about but I do anticipate these types of languages will become more common in introductory computer courses.

NathanG
Post 1

Declarative programming requires a shift in the way that you think. Examples of declarative programming language technologies include Windows Presentation Foundation, which is the new technology from Microsoft that is supposed to be the de facto standard for future Windows development.

Windows Presentation Foundation is completely XML-based, which makes it a little like programming in HTML. That can be a bit disorienting at first. However, the philosophy behind it is to separate function and design. A Windows “form” in the WPF model is an XML document that a designer can modify easily and then send back to the developer to update the code. There is no binary form file as there used to be in the other Rapid Application Development tools.

Post your comments

exception 'Exception' with message 'error writing captcha: Duplicate entry '2147483647' for key 'PRIMARY'' in /ssd/www/wisegeek/public_html/_core/classes/public/Captcha.php:44
Stack trace:
#0 /ssd/www/wisegeek/public_html/_core/controls/public/ControlDiscussionPostBox.php(324): Captcha->createCaptcha()
#1 /ssd/www/wisegeek/public_html/framework/classes/Control.php(104): ControlDiscussionPostBox->preRender(false)
#2 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): Control->render()
#3 /ssd/www/wisegeek/public_html/tpl/default-nocustom-lu/pages/public/article/article.htm(526): Control->__toString()
#4 /ssd/www/wisegeek/public_html/framework/classes/Control.php(300): require('/ssd/www/wisege...')
#5 /ssd/www/wisegeek/public_html/framework/classes/Control.php(309): Control->requireTpl('pages/public/ar...', Object(PageArticleCom), true)
#6 /ssd/www/wisegeek/public_html/framework/classes/Control.php(131): Control->renderTpl('pages/public/ar...', Object(PageArticleCom))
#7 /ssd/www/wisegeek/public_html/framework/classes/FormDataControl.php(87): Control->renderTemplate()
#8 /ssd/www/wisegeek/public_html/framework/classes/Control.php(109): FormDataControl->renderTemplate()
#9 /ssd/www/wisegeek/public_html/framework/classes/ScriptPage.php(50): Control->render(false)
#10 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): ScriptPage->render()
#11 /ssd/www/wisegeek/public_html/framework/classes/Page.php(97): Control->__toString()
#12 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(443): Page->processRequest()
#13 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(7): PublicFrontController->renderPage()
#14 /ssd/www/wisegeek/public_html/index.php(11): PublicFrontController::run()
#15 {main}