Content deleted Content added
→Examples: Added Scheme along with a factorial example for functional programming. Ideally someone should rewrite this and add haskell as well as a second example. |
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. |
||
(34 intermediate revisions by 25 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]]
{{Programming paradigms}}▼
▲In [[computer science]], '''declarative programming''' is a [[programming paradigm]]—a style of building the structure and elements of computer programs—that expresses the logic of a [[computation]] without describing its [[control flow]].<ref>{{citation|last=Lloyd|first=J.W.|title=Practical Advantages of Declarative Programming}}</ref>
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]],
== Definition ==▼
▲==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:
* 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 33 ⟶ 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 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.
==Examples==
===
[[Lisp (programming language)|Lisp]] is a family of programming languages loosely inspired by mathematical notation and [[Alonzo Church]]'s [[lambda calculus]]. Some dialects, such as [[Common Lisp]], are primarily imperative but support functional programming. Others, such as [[Scheme (programming language)|Scheme]], are designed for functional programming.
▲For example, one may define the [[factorial]] function in scheme as such:
<syntaxhighlight lang=scheme>
(define (factorial n)
(if (= n 0)
1 ;;; 0! = 1
(* n (factorial (- n 1))))) ;;; n! = n*(n-1)!
</syntaxhighlight>
This defines the factorial function using its recursive definition. In contrast, it is more typical to define a procedure for an imperative language.
In lisps and lambda calculus, functions are generally [[first-class citizen]]s. Loosely, this means that functions can be inputs and outputs for other functions. This can simplify the definition of some functions.
For example, writing a function to output the first n [[square number]]s in [[Racket (programming language)|Racket]] can be done accordingly:
<syntaxhighlight lang=scheme>
(define (first-n-squares n)
(map (lambda (x) (* x x)) ;;; A function mapping x -> x^2
(range n))) ;;; Lists the first n naturals
</syntaxhighlight>
The [[Map (higher-order function)|map]] function accepts a function and a list; the output is a list of results of the input function on each element of the input list.
===ML===
[[ML (programming language)|ML]] (1973)<ref name="Gordon1996">{{cite web
|last1=Gordon
|first1=Michael J. C.
|author1-link=Michael J. C. Gordon
|year=1996
|title=From LCF to HOL: a short history
|url=http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html
|access-date=2021-10-30
|archive-date=2016-09-05
|archive-url=https://web.archive.org/web/20160905201847/http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html
|url-status=live
}}</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
|last1=Wilson
|first1=Leslie B.
|year=2001
|title=Comparative Programming Languages, Third Edition
|publisher=Addison-Wesley
|page=233
|isbn=0-201-71012-9
}}</ref>
{{sxhl|2=sml|1=fun times_10(n : int) : int = 10 * n;}}
''ML'' is not as bracket-centric as ''Lisp'', and instead uses a wider variety of syntax to codify the relationship between code elements, rather than appealing to list ordering and nesting to express everything. The following is an application of <code>times_10</code>:
times_10 2
It returns "20 : int", that is, <code>20</code>, a value of type <code>int</code>.
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
|last=Wilson
|first=Leslie B.
|year=2001
|title=Comparative Programming Languages, Third Edition
|publisher=Addison-Wesley
|page=235
|isbn=0-201-71012-9
}}</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 98 ⟶ 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 |
<syntaxhighlight lang="prolog">
animal(tom).
Line 121 ⟶ 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 139 ⟶ 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 146 ⟶ 199:
* Olof Torgersson. [https://web.archive.org/web/20060330033506/http://www.cs.chalmers.se/~oloft/Papers/wm96/wm96.html A Note on Declarative Programming Paradigms and the Future of Definitional Programming]. 1996.
▲{{Programming paradigms navbox}}
{{Types of programming languages}}
{{Authority control}}
Line 151 ⟶ 205:
[[Category:Declarative programming| ]]
[[Category:Programming paradigms]]
<!-- Hidden categories below -->
[[Category:Articles with example Lisp (programming language) code]]
|