Declarative programming
From Wikipedia, the free encyclopedia
It has been suggested that this article or section be merged with Declarative programming language. (Discuss) |
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:
- XSLT is a declarative language for transforming XML documents
- SQL has a declarative part for relational database queries
- LINQ is an extension of the .NET languages for specifying queries on objects and other data sources
- TK Solver
- XForms is an XML format for the specification of a data processing model for XML data and user interface(s) for the XML data, such as web forms.
- XAML is a Microsoft specification language for User interfaces.
- XUL is a Mozilla specification language for user interfaces.
- MXML is a Adobe specification language for User interfaces.
Functional/logic/constraint programming languages that support declarative programming:
- Functional: Erlang, Haskell and Lisp
- Logic: Prolog, Mercury
- Constraint: Oz, several prolog extensions (like ECLiPSe or SICSTUS)
[edit] See also
- Declarative programming language for programming languages that support declarative programming
- Computer programming for programming in general
- Programming paradigm for programming paradigms in general
- Imperative programming which in contrast concentrates on manipulating state
- Procedural programming which in contrast concentrates on sequences of steps to follow
- Business rules
- Domain-specific programming language
[edit] References
- Declarative language in The Free On-line Dictionary of Computing, Editor Denis Howe.
- Relational language in The Free On-line Dictionary of Computing, Editor Denis Howe.
- Logic programming in The Free On-line Dictionary of Computing, Editor Denis Howe.
- Functional programming in The Free On-line Dictionary of Computing, Editor Denis Howe.
[edit] External links
- Open source project for functional declarative javascript
- Frans Coenen. Characteristics of declarative programming languages. 1999.
- Olof Torgersson. A Note on Declarative Programming Paradigms and the Future of Definitional Programming. 1996.
- David Mertz. Declarative Programming with XML Stylesheet Language Transformations. 2001.
- David Mertz. Advanced OOP: Declarative Programming and Mini-Languages. 2003-07-31.
- Narayanan Jayaratchagan. Declarative Programming in Java. 2004-04-21.
- N. Alex Rupp. Ruling Out: Rule Engines and Declarative Programming Come to Java. 2004-08-19.