Content deleted Content added
HeyElliott (talk | contribs) |
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. |
||
(39 intermediate revisions by 27 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]], a style of building the structure and elements of computer programs, that expresses the logic of a [[computation]] without describing its [[control flow]].<ref>{{cite report |last1=Lloyd |first1=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 ==
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}}
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===
{{Further|Domain-specific language}}
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===
{{Further|Functional programming}}
Functional programming languages such as [[Haskell]], [[Scheme (programming language)|Scheme]], and [[ML (programming language)|ML]] evaluate expressions via function application. Unlike the related but more imperative paradigm of [[procedural programming]], functional programming places little emphasis on explicit sequencing. Instead, computations are characterised by various kinds of recursive [[higher-order function]] application and [[Function composition (computer science)|composition]], and as such can be regarded simply as a set of mappings between [[Domain of a function|domains]] and [[codomain]]s. Many functional languages, including most of those in the ML and Lisp families, are not [[purely functional programming|purely functional]], and thus allow introducing [[Side effect (computer science)|stateful effects]] in programs.
===Hybrid languages===
Line 50:
===Logic programming===
{{Further|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|SAT solvers]] to generate a model of the program.
===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===
[[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.
In Scheme, the [[factorial]] function can be defined as follows:
<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
|
|
|
|
|
|
|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>
Line 151 ⟶ 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
|title=Comparative Programming Languages, Third Edition
|publisher=Addison-Wesley
}}</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
|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 184 ⟶ 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 207 ⟶ 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 225 ⟶ 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 232 ⟶ 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 237 ⟶ 205:
[[Category:Declarative programming| ]]
[[Category:Programming paradigms]]
<!-- Hidden categories below -->
[[Category:Articles with example Lisp (programming language) code]]
|