Content deleted Content added
Aarghdvaark (talk | contribs) same thing said twice with different refs. Deleted one instance but kept ref |
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. |
||
(25 intermediate revisions by 20 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===
{{Further|Constraint programming}}
===Domain-specific languages===
{{Further|Domain-specific language}}
Well-known examples of 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===
{{Further|Functional programming}}
Functional programming languages such as [[
===Hybrid languages===
Line 47 ⟶ 50:
===Logic programming===
{{Further|Logic programming}}
===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}}</ref>
==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 68 ⟶ 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
|
|
|
|
|
|
|access-date=2021-10-30
| access-date = 2021-10-30}}</ref> stands for "Meta Language." ML is statically typed, and function arguments and return types may be annotated.<ref name="cpl_3rd-ch9-233">{{cite book▼
|archive-date=2016-09-05
| last = Wilson▼
|archive-url=https://web.archive.org/web/20160905201847/http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html
| first = Leslie B.▼
|url-status=live
| title = Comparative Programming Languages, Third Edition▼
▲
| publisher = Addison-Wesley▼
| year = 2001▼
| page = 233▼
| isbn = 0-201-71012-9▼
}}</ref>
Line 112 ⟶ 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
|access-date=2022-05-25
}}</ref> using SL resolution<ref>{{cite journal |author1=Robert Kowalski |author2=Donald Kuehner |url=http://www.doc.ic.ac.uk/~rak/papers/sl.pdf |title=Linear Resolution with Selection Function |journal=Artificial Intelligence |issn=0004-3702 |volume=2 |issue=3–4 |date=Winter 1971 |pages=227–260 |doi=10.1016/0004-3702(71)90012-9}}</ref> both to deduce answers to queries and to parse and generate natural language sentences.▼
|archive-date=2015-04-02
|archive-url=https://web.archive.org/web/20150402111123/http://alain.colmerauer.free.fr/alcol/ArchivesPublications/PrologHistory/19november92.pdf
|url-status=live
▲ }}</ref> using SL resolution<ref>{{cite journal |author1=Robert Kowalski |author2=Donald Kuehner |url=http://www.doc.ic.ac.uk/~rak/papers/sl.pdf |title=Linear Resolution with Selection Function |journal=Artificial Intelligence |issn=0004-3702 |volume=2 |issue=3–4 |date=Winter 1971 |pages=227–260 |doi=10.1016/0004-3702(71)90012-9 |access-date=2023-08-13 |archive-date=2015-09-23 |archive-url=https://web.archive.org/web/20150923215814/http://www.doc.ic.ac.uk/~rak/papers/sl.pdf |url-status=live}}</ref> both to deduce answers to queries and to parse and generate natural language sentences.
The building blocks of a Prolog program are ''facts'' and ''rules''. Here is a simple example:
Line 146 ⟶ 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 169 ⟶ 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 187 ⟶ 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 200 ⟶ 205:
[[Category:Declarative programming| ]]
[[Category:Programming paradigms]]
<!-- Hidden categories below -->
[[Category:Articles with example Lisp (programming language) code]]
|