Content deleted Content added
Rescuing 9 sources and tagging 0 as dead.) #IABot (v2.0.9.5 |
Jerryobject (talk | contribs) Template:More footnotes update > Template:More footnotes needed. Template:Refimprove update > Template:More citations needed. WP:REFerence WP:CITation parameters: respaces, cut whitespace characters to standardize, aid work via small screens, reorders, update-standardize. WP:LINKs: update-standardizes, needless-WP:PIPEs > WP:NOPIPEs, add, underscores > spaces. Template:Main change > Template:Further. Adds: MOS:COMMENT, WP:CATEGORY. |
||
(23 intermediate revisions by 18 users not shown) | |||
Line 1:
{{Short description|Programming paradigm based on modeling the logic of a computation}}
{{
{{More footnotes needed|date=April 2010}}
{{
In [[computer science]], '''declarative programming''' is a [[programming paradigm]]
Many languages that apply this style attempt to minimize or eliminate [[
Declarative programming often considers [[program (machine)|programs]] as theories of a [[
Common declarative languages include those of [[Query languages|database query languages]] (e.g., [[SQL]], [[XQuery]]), [[regular expression]]s, [[logic programming]] (e.g., [[Prolog]], [[Datalog]], [[answer set programming]]), [[functional programming]], [[Configuration management database|configuration management]], and [[Algebraic modeling language|algebraic modeling]] systems.
== Definition ==
Declarative programming is often defined as any style of programming that is not imperative. A number of other common definitions attempt to define it by simply contrasting it with imperative programming. For example:▼
▲Declarative programming is often defined as any style of programming that is not imperative. A number of other common definitions attempt to define it by simply contrasting it with imperative programming. For example:
* A high-level program that describes what a computation should perform.
* Any programming language that lacks [[
* A language with a clear correspondence to [[mathematical logic]].<ref>{{cite thesis |
These definitions overlap substantially.{{citation needed|date=July 2025}}
Declarative programming is a non-imperative style of programming in which programs describe their desired results without explicitly listing commands or steps that must be performed. Functional and logic programming languages are characterized by a declarative programming style. In logic programming, programs consist of sentences expressed in logical form, and computation uses those sentences to solve problems, which are also expressed in logical form.
In a [[pure functional language]], such as [[
Some logic programming languages, such as [[Prolog]], and database query languages, such as SQL, while declarative in principle, also support a procedural style of programming.{{citation needed|date=July 2025}}
==Subparadigms==
Line 30:
===Constraint programming===
{{
Constraint programming states relations between variables in the form of constraints that specify the properties of the target solution. The set of constraints is [[solver (computer science)|solved]] by giving a value to each variable so that the solution is consistent with the maximum number of constraints. Constraint programming often complements other paradigms: functional, logical, or even imperative programming.
===Domain-specific languages===
{{
Well-known examples of declarative ___domain-specific languages (DSLs) include the [[yacc]] parser generator input language, [[QML]], the [[Make (software)|Make]] build specification language, [[Puppet (software)|Puppet]]'s configuration management language, [[regular expression]]s, [[Datalog]], [[answer set programming]] and a subset of [[SQL]] (SELECT queries, for example). DSLs have the advantage of being useful while not necessarily needing to be [[Turing-complete]], which makes it easier for a language to be purely declarative.
Many markup languages such as [[HTML]], [[MXML]], [[XAML]], [[XSLT]] or other [[user-interface markup language]]s are often declarative. HTML, for example, only describes what should appear on a webpage - it specifies neither [[control flow]] for rendering a page nor the page's possible [[human-computer interaction|interactions with a user]].
{{As of
===Functional programming===
{{
Functional programming languages such as [[
===Hybrid languages===
Line 50:
===Logic programming===
{{
Logic programming languages, such as [[Prolog]], [[Datalog]] and [[answer set programming]], compute by proving that a goal is a logical consequence of the program, or by showing that the goal is true in a model defined by the program. Prolog computes by reducing goals to subgoals, top-down using [[backward chaining |backward reasoning]], whereas most Datalog systems compute bottom-up using [[forward chaining |forward reasoning]]. Answer set programs typically use [[Boolean SAT solver
===Modeling===
{{
Models, or mathematical representations, of physical systems may be implemented in computer code that is declarative. The code contains a number of equations, not imperative assignments, that describe ("declare") the behavioral relationships. When a model is expressed in this formalism, a computer is able to perform algebraic manipulations to best formulate the solution algorithm. The mathematical causality is typically imposed at the boundaries of the physical system, while the behavioral description of the system itself is declarative or acausal. Declarative [[modeling language]]s and environments include [[Analytica (software)|Analytica]], [[Modelica]] and [[Simile (computing)|Simile]].<ref>{{cite web |url=http://www.simulistics.com/tour/declarative.htm |title=Declarative modelling |publisher=Simulistics |access-date=15 August 2013 |archive-date=11 August 2003 |archive-url=https://web.archive.org/web/20030811073949/http://www.simulistics.com/tour/declarative.htm |url-status=live
==Examples==
===Lisp===
[[Lisp (programming language)
▲[[Lisp (programming language) | Lisp]] is a family of programming languages loosely inspired by mathematical notation and [[Alonzo Church]]'s [[lambda calculus]]. While some dialects, such as [[Common Lisp]] are primarily imperative, these lisps support functional programming, and other lisps, such as [[Scheme (programming language) |Scheme]] are designed for functional programming.
In
<syntaxhighlight lang=scheme>
Line 72 ⟶ 71:
</syntaxhighlight>
This
In lisps
For example,
▲For example, if one wants to create a function that returns the first n [[square numbers |squares]] in [[Racket (programming language) |Racket]], one can simply write the following:
<syntaxhighlight lang=scheme>
(define (first-n-squares n)
(map (lambda (x) (* x x)) ;;; A function mapping x -> x^2
</syntaxhighlight>
The [[Map (higher-order function)
===ML===
[[ML (programming language)|ML]] (1973)<ref name="Gordon1996">{{cite web
|
|
|
|
|
|
|
|
|
|
▲ | year = 2001
}}</ref>
Line 120 ⟶ 116:
Like ''Lisp'', ''ML'' is tailored to process lists, though all elements of a list must be the same type.<ref name="cpl_3rd-ch9-235">{{cite book
▲ | year = 2001
}}</ref>
===Prolog===
[[Prolog]] (1972) stands for "PROgramming in LOGic." It was developed for natural language [[question answering]],<ref name="PrologHistory">{{cite web
The building blocks of a Prolog program are ''facts'' and ''rules''. Here is a simple example:
Line 158 ⟶ 153:
Given this program, the query <syntaxhighlight inline lang=prolog>eat(tom,jerry)</syntaxhighlight> succeeds, while <syntaxhighlight inline lang=prolog>eat(jerry,tom)</syntaxhighlight> fails. Moreover, the query <syntaxhighlight inline lang=prolog>eat(X,jerry)</syntaxhighlight> succeeds with the answer substitution <syntaxhighlight inline lang=prolog>X=tom</syntaxhighlight>.
Prolog executes programs top-down, using [[SLD resolution]] to [[backward chaining
The broad range of Prolog applications is highlighted in the Year of Prolog Book,<ref name="Prolog Book">{{cite book |last1=Warren |first1=D.S. |
===Datalog===
The [[Datalog#History
Most Datalog systems execute programs bottom-up, using rules to [[forward chaining |reason forwards]], deriving new facts from existing facts, and terminating when there are no new facts that can be derived, or when the derived facts unify with the query. In the above example, a typical Datalog system would first derive the new facts:
Line 181 ⟶ 176:
eats(X, jerry).</syntaxhighlight>
Datalog has been applied to such problems as [[data integration]], [[information extraction]], [[Computer network|networking]], [[security]], [[cloud computing]] and [[machine learning]].<ref>{{cite conference |last1=Huang
=== Answer
[[Answer set programming]] (ASP) evolved in the late 1990s, based on the [[stable model semantics|stable model]] (answer set) semantics of logic programming. Like Datalog, it is a subset of Prolog; and, because it
Most implementations of ASP execute a program by first
Its applications are oriented towards solving difficult [[search algorithm|search problems]] and [[knowledge representation]].<ref>{{cite book |
==See also==
* [[Inductive programming]]
* [[List of programming languages by type#Declarative_languages|List of declarative programming languages]]
Line 199 ⟶ 193:
==External links==
* Frans Coenen. [https://web.archive.org/web/20060424045449/http://www.csc.liv.ac.uk/~frans/OldLectures/2CS24/declarative.html#detail Characteristics of declarative programming languages]. 1999.
*[[Robert Harper (computer scientist)|Robert Harper]].
Line 212 ⟶ 205:
[[Category:Declarative programming| ]]
[[Category:Programming paradigms]]
<!-- Hidden categories below -->
[[Category:Articles with example Lisp (programming language) code]]
|