Declarative programming

From Wikipedia, the free encyclopedia

Declarative programming is a term with two distinct meanings, both of which are in current use.

According to one definition, a program is "declarative" if it describes what something is like, rather than how to create it. For example, HTML web pages are declarative because they describe what the page should contain, but not how to actually display the page on a computer screen. This is a different approach from imperative programming languages, which require the programmer to specify an algorithm to be run.

According to a different definition, a program is "declarative" if it is written in a purely functional programming language, logic programming language, or constraint programming language. The phrase "declarative language" is sometimes used to describe all such programming languages as a group, and to contrast them against imperative languages.

Contents

[edit] Definition Overlap

The two definitions of "declarative programming" overlap somewhat. In particular, constraint programming and, to a lesser degree, logic programming focus on describing the properties of the desired solution (the what), leaving unspecified the actual algorithm that should be used to find that solution (the how). However, most logic and constraint languages are able to describe algorithms and implementation details, so they are not strictly declarative by the first definition.

Similarly, it is possible to write programs in a declarative style even in an imperative programming language. This is usually done by encapsulating non-declarative details inside a library or framework. An example of this style is the use of reflection in the JUnit unit test framework, which allows unit tests to be registered with the framework merely by being defined.

In a declarative program you write (declare) a data structure that is processed by a standard algorithm (for that language) to produce the desired result.

A declarative language, like all languages, has a syntax describing how the words in the language may be combined, and a semantics describing how sentences in the language correspond to a program's output.

[edit] Domain-specific languages

One popular form of declarative programming is its use in domain-specific languages (DSLs). "Domain" in this case refers to the "domain of discourse" — the subject matter that the language is intended to describe. Examples of DSLs are configuration files, spreadsheets, and even email (the "From:" and "To:" headers can be seen as processing instructions).

One drawback of DSLs is that they are often not Turing-complete. That is, there are certain things they cannot do. Just as you cannot use a spreadsheet to send e-mail, you cannot use e-mail to calculate your bank balance. For this reason, DSLs are sometimes embedded inside a general-purpose programming language. This allows a programmer to use the DSL for problems that play to its strengths, and use the general purpose language for problems that are difficult or impossible in the DSL.

Stand-alone DSLs — those not embedded in a general purpose language — are often easier to use than an embedded equivalent, since they avoid the added conceptual complexity imposed by the general purpose language.

[edit] Examples

Category:Declarative programming languages tries to provide an exhaustive list.

Frameworks whose deployment has declarative programming aspects:

Domain-specific languages that support declarative programming:

Functional/logic/constraint programming languages that support declarative programming:

[edit] See also

[edit] References

[edit] External links