{{Short description|Programming paradigm based on modeling the logic of a computation}} {{Multiple issues| {{More footnotes needed|date=April 2010}} {{More citations needed|date=February 2015}} {{Rewrite|date=December 2025}} }} In computer science, '''declarative programming''' is a programming paradigm that expresses the logic of a computation without describing its control flow.<ref>{{cite conference |last1=Lloyd |first1=J.W. |title=Practical Advantages of Declarative Programming |book-title=GULP-PRODE'94 1994 Joint Conference on Declarative Programming |volume=I |pages=3-17 |editor-first1=María |editor-last1=Alpuente |editor-first2=Roberto |editor-last2=Barbuti |editor-first3=Isidro |editor-last3=Ramos |location=Peñíscola, Spain |date=September 19–22, 1994 |url=https://www.programmazionelogica.it/wp-content/uploads/2015/12/GP1994-I-000-031.pdf}}</ref>
Many languages that apply this style attempt to minimize or eliminate side effects by describing ''what'' the program must accomplish in terms of the problem domain, rather than describing ''how'' to accomplish it as a sequence of the programming language primitives<ref name="FOLDOC 2004">{{cite web |title=declarative language |website=FOLDOC |date=17 May 2004 |url=https://foldoc.org/declarative+language |access-date=7 September 2023 |archive-date=7 September 2023 |archive-url=https://web.archive.org/web/20230907151526/https://foldoc.org/declarative+language |url-status=live}}</ref> (the ''how'' being left up to the language's implementation). This is in contrast with imperative programming, which implements algorithms in explicit steps.<ref name="Sebesta 2016">{{cite book |last1=Sebesta |first1=Robert |title=Concepts of programming languages |publisher=Pearson |publication-place=Boston |year=2016 |isbn=978-0-13-394302-3 |oclc=896687896}}</ref><ref>{{Cite web |date=2021-05-21 |title=Imperative programming: Overview of the oldest programming paradigm |url=https://www.ionos.com/digitalguide/websites/web-development/imperative-programming/ |access-date=2023-05-23 |website=IONOS Digital Guide |language=en-US |archive-date=2022-05-03 |archive-url=https://web.archive.org/web/20220503083342/https://www.ionos.com/digitalguide/websites/web-development/imperative-programming/ |url-status=live}}</ref>
Declarative programming often considers programs as theories of a formal logic, and computations as deductions in that logic space. Declarative programming may greatly simplify writing parallel programs.<ref>{{cite web |url=http://www.cse.unsw.edu.au/~pls/damp09/ |title=DAMP 2009: Workshop on Declarative Aspects of Multicore Programming |publisher=Cse.unsw.edu.au |date=20 January 2009 |access-date=15 August 2013 |archive-url=https://web.archive.org/web/20130913162703/http://www.cse.unsw.edu.au/~pls/damp09/ |archive-date=13 September 2013 |url-status=dead}}</ref>
Common declarative languages include those of database query languages (e.g., SQL, XQuery), regular expressions, logic programming (e.g., Prolog, Datalog, answer set programming), functional programming, configuration management, and 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: * A high-level program that describes what a computation should perform. * Any programming language that lacks side effects, or more specifically, has referential transparency. * A language with a clear correspondence to mathematical logic.<ref>{{cite thesis |first1=Manuel M. T. |last1=Chakravarty |date=14 February 1997 |url=http://www.cse.unsw.edu.au/~chak/papers/diss.ps.gz |title=On the Massively Parallel Execution of Declarative Programs |type=Doctoral dissertation |publisher=Technische Universität Berlin |access-date=26 February 2015 |quote=In this context, the criterion for calling a programming language declarative is the existence of a clear, mathematically established correspondence between the language and mathematical logic such that a declarative semantics for the language can be based on the model or the proof theory (or both) of the logic. |archive-date=23 September 2015 |archive-url=https://web.archive.org/web/20150923211531/http://www.cse.unsw.edu.au/~chak/papers/diss.ps.gz |url-status=live}}</ref><!-- this citation is for this bullet point only -->
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. Logic programming languages are characterized by a mostly 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 Haskell, all functions are without side effects, and state changes are only represented as functions that transform the state, which is explicitly represented as a first-class object in the program. Although pure functional languages are non-imperative, they often provide a facility for describing the effect of a function as a series of steps. Other functional languages, such as Lisp, OCaml and Erlang, support a mixture of procedural and functional programming.{{citation needed|date=July 2025}}
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== Declarative programming is an umbrella term that includes a number of better-known programming paradigms.
===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 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 build specification language, Puppet's configuration management language, regular expressions, 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 languages 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 interactions with a user.
{{As of|2013}}, some software systems{{Which|date=July 2020}} combine traditional user-interface markup languages (such as HTML) with declarative markup that defines what (but not how) the back-end server systems should do to support the declared interface. Such systems, typically using a domain-specific XML namespace, may include abstractions of SQL database syntax or parameterized calls to web services using representational state transfer (REST) and SOAP.{{Citation needed|date=July 2020}}
===Functional programming=== {{Further|Functional programming}} Functional programming languages such as Haskell, Scheme, and 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 composition, and as such can be regarded simply as a set of mappings between domains and codomains. Many functional languages, including most of those in the ML and Lisp families, are not purely functional, and thus allow introducing stateful effects in programs.
===Hybrid languages=== {{see also|Multi-paradigm programming language}} Makefiles, for example, specify dependencies in a declarative fashion,<ref>{{cite web |url=http://phoenix.labri.fr/wiki/doku.php?id=an_overview_on_dsls |title=An overview on dsls |archive-url=https://web.archive.org/web/20071023021126/http://phoenix.labri.fr/wiki/doku.php?id=an_overview_on_dsls |archive-date=October 23, 2007 |url-status=dead}}</ref> but include an imperative list of actions to take as well. Similarly, yacc specifies a context free grammar declaratively, but includes code snippets from a host language, which is usually imperative (such as C).
===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 reasoning, whereas most Datalog systems compute bottom-up using forward reasoning. Answer set programs typically use SAT solvers to generate a model of the program.
===Modeling=== {{Further|Mathematical model}} 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 languages and environments include Analytica, Modelica and 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==
===ML=== 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 |url=http://alain.colmerauer.free.fr/alcol/ArchivesPublications/PrologHistory/19november92.pdf |title=Birth of Prolog |date=November 1992 |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: <syntaxhighlight lang=prolog> cat(tom). % tom is a cat mouse(jerry). % jerry is a mouse
animal(X) :- cat(X). % each cat is an animal animal(X) :- mouse(X). % each mouse is an animal
big(X) :- cat(X). % each cat is big small(X) :- mouse(X). % each mouse is small
eat(X,Y) :- mouse(X), cheese(Y). % each mouse eats each cheese eat(X,Y) :- big(X), small(Y). % each big being eats each small being </syntaxhighlight>
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 reason backwards, reducing goals to subgoals. In this example, it uses the last rule of the program to reduce the goal of answering the query <syntaxhighlight inline lang=prolog>eat(X,jerry)</syntaxhighlight> to the subgoals of first finding an X such that <syntaxhighlight inline lang=prolog>big(X)</syntaxhighlight> holds and then of showing that <syntaxhighlight inline lang=prolog>small(jerry)</syntaxhighlight> holds. It repeatedly uses rules to further reduce subgoals to other subgoals, until it eventually succeeds in unifying all subgoals with facts in the program. This backward reasoning, goal-reduction strategy treats rules in logic programs as procedures, and makes Prolog both a declarative and procedural programming language.<ref>Robert Kowalski [http://www.doc.ic.ac.uk/~rak/papers/IFIP%2074.pdf Predicate Logic as a Programming Language] {{Webarchive|url=https://web.archive.org/web/20160207012437/http://www.doc.ic.ac.uk/~rak/papers/IFIP%2074.pdf |date=2016-02-07}} Memo 70, Department of Artificial Intelligence, University of Edinburgh. 1973. Also in Proceedings IFIP Congress, Stockholm, North Holland Publishing Co., 1974, pp. 569-574.</ref>
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. |editor1-last=Warren |editor1-first=D.S. |editor2-last=Dahl |editor2-first=V. |editor3-last=Eiter |editor3-first=T. |editor4-last=Hermenegildo |editor4-first=M.V. |editor5-last=Kowalski |editor5-first=R. |editor6-last=Rossi |editor6-first=F. |date=2023 |chapter=Introduction to Prolog |title=Prolog: The Next 50 Years |series=Lecture Notes in Computer Science (LNCS) |volume=13900 |publisher=Springer, Cham. |doi=10.1007/978-3-031-35254-6_1 |pages=3–19 |isbn=978-3-031-35253-9}}</ref> celebrating the 50 year anniversary of Prolog.
===Datalog=== The origins of Datalog date back to the beginning of logic programming, but it was identified as a separate area around 1977. Syntactically and semantically, it is a subset of Prolog. But because it lacks compound terms, it is not Turing-complete.{{Citation needed|date=December 2025}}
Most Datalog systems execute programs bottom-up, using rules to 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.{{cn|date=December 2025}}
Datalog has been applied to such problems as data integration, information extraction, networking, security, cloud computing and machine learning.<ref>{{cite conference |last1=Huang |first1=Shan Shan |last2=Green |first2=Todd J. |last3=Loo |first3=Boon Thau |date=June 12–16, 2011 |url=https://www.cs.ucdavis.edu/~green/papers/sigmod906t-huang.pdf |conference=SIGMOD 2011 |title=Datalog and Emerging applications |publisher=Association for Computing Machinery |location=Athens, Greece |isbn=978-1-4503-0661-4 |access-date=2023-08-13 |archive-date=2020-10-22 |archive-url=https://web.archive.org/web/20201022234145/https://www.cs.ucdavis.edu/~green/papers/sigmod906t-huang.pdf |url-status=live}}</ref><ref>{{Cite conference |last1=Mei |first1=Hongyuan |last2=Qin |first2=Guanghui |last3=Xu |first3=Minjie |last4=Eisner |first4=Jason |year=2020 |title=Neural Datalog Through Time: Informed Temporal Modeling via Logical Specification |book-title=Proceedings of ICML 2020 |arxiv=2006.16723}}</ref>
=== Answer set programming=== Answer set programming (ASP) evolved in the late 1990s, based on the stable model (answer set) semantics of logic programming. Like Datalog, it is a subset of Prolog; and, because it lacks compound terms, it is not Turing-complete.
Most implementations of ASP execute a program by first ''grounding'' the program, replacing all variables in rules by constants in all possible ways, and then using a propositional SAT solver, such as the DPLL algorithm to generate one or more models of the program.
Its applications are oriented towards solving difficult search problems and knowledge representation.<ref>{{cite book |first1=Chitta |last1=Baral |title=Knowledge Representation, Reasoning and Declarative Problem Solving |url=https://archive.org/details/knowledgereprese00bara |url-access=registration |year=2003 |publisher=Cambridge University Press |isbn=978-0-521-81802-5}}</ref><ref>{{cite book |first1=Michael |last1=Gelfond |chapter=Answer sets |editor1-first=Frank |editor1-last=van Harmelen |editor2-first=Vladimir |editor2-last=Lifschitz |editor3-first=Bruce |editor3-last=Porter |year=2008 |title=Handbook of Knowledge Representation |chapter-url=https://books.google.com/books?id=xwBDylHhJhYC&pg=PA285 |publisher=Elsevier |pages=285–316 |isbn=978-0-08-055702-1}} [http://www.depts.ttu.edu/cs/research/krlab/pdfs/papers/gel07b.pdf as PDF] {{Webarchive|url=https://web.archive.org/web/20160303231241/http://www.depts.ttu.edu/cs/research/krlab/pdfs/papers/gel07b.pdf |date=2016-03-03}}</ref>
==See also== * Inductive programming * List of declarative programming languages
==References== {{Reflist}}
==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. **[https://existentialtype.wordpress.com/2013/07/18/what-if-anything-is-a-declarative-language/ What, If Anything, Is A Declarative Language?]. 2013. **"[https://existentialtype.wordpress.com/2013/07/22/there-is-such-a-thing-as-a-declarative-language/ There Is Such A Thing As A Declarative Language, and It's The World's Best DSL]". 2013. * 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}}
Category:Declarative programming Category:Programming paradigms <!-- Hidden categories below --> Category:Articles with example Lisp (programming language) code