Declarative programming
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
|
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.[1]
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[2] (the how being left up to the language's implementation). This is in contrast with imperative programming, which implements algorithms in explicit steps.[3][4]
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.[5]
Common declarative languages include those of
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, is referentially transparent).
- A language with a clear correspondence to mathematical logic.[6]
These definitions overlap substantially.
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
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.
Subparadigms
Declarative programming is an
Constraint programming
Domain-specific languages
Well-known examples of declarative
Many markup languages such as
As of 2013[update], some software systems[
Functional programming
Functional programming languages such as
Hybrid languages
Makefiles, for example, specify dependencies in a declarative fashion,[7] 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
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 languages and environments include Analytica, Modelica and Simile.[8]
Examples
Lisp
This section may require copy editing for checking relevance and factual accuracy. (December 2023) |
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 are designed for functional programming.
In scheme, one may define the factorial function as such:
(define (factorial n)
(if (= n 0)
1 ;;; 0! = 1
(* n (factorial (- n 1))))) ;;; n! = n*(n-1)!
This can be construed as defining the factorial function using its recursive mathematical definition, as opposed to simply defining a procedure, as one would typically do in an imperative language.
In lisps, as in lambda calculus, functions are generally first-class citizens. Loosely, this means that functions can return functions, and be used as parameters for other functions.
This can greatly simplify the definition of certain functions.
For example, if one wants to create a function that returns the first n
(define (first-n-squares n)
(map
(lambda (x) (* x x)) ;;; A function mapping x -> x^2
(range n))) ;;; List of the first n non-negative integers
Map in the above takes a function and a list, applying the function to each member of the list.
ML
ML (1973)[9] stands for "Meta Language." ML is statically typed, and function arguments and return types may be annotated.[10]
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 times_10
:
times_10 2
It returns "20 : int", that is, 20
, a value of type int
.
Like Lisp, ML is tailored to process lists, though all elements of a list must be the same type.[11]
Prolog
Prolog (1972) stands for "PROgramming in LOGic." It was developed for natural language question answering,[12] using SL resolution[13] 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:
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
Given this program, the query eat(tom,jerry)
succeeds, while eat(jerry,tom)
fails. Moreover, the query eat(X,jerry)
succeeds with the answer substitution X=tom
.
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 eat(X,jerry)
to the subgoals of first finding an X such that big(X)
holds and then of showing that small(jerry)
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.[14]
The broad range of Prolog applications is highlighted in the Year of Prolog Book,[15] 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 does not have compound terms, it is not Turing-complete.
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. In the above example, a typical Datalog system would first derive the new facts:
animal(tom).
animal(jerry).
big(tom).
small(jerry).
Using these facts, it would then derive the additional fact:
eats(tom, jerry).
It would then terminate, both because no new, additional facts can be derived, and because the newly derived fact unifies with the query
eats(X, jerry).
Datalog has been applied to such problems as data integration, information extraction, networking, security, cloud computing and machine learning.[16][17]
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 does not have 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
See also
References
- ^ Lloyd, J.W., Practical Advantages of Declarative Programming
- ^ "declarative language". FOLDOC. 17 May 2004. Retrieved 7 September 2023.
- OCLC 896687896.
- ^ "Imperative programming: Overview of the oldest programming paradigm". IONOS Digital Guide. 2021-05-21. Retrieved 2023-05-23.
- ^ "DAMP 2009: Workshop on Declarative Aspects of Multicore Programming". Cse.unsw.edu.au. 20 January 2009. Archived from the original on 13 September 2013. Retrieved 15 August 2013.
- ^ Chakravarty, Manuel M. T. (14 February 1997). On the Massively Parallel Execution of Declarative Programs (Doctoral dissertation). Technical University of Berlin. Retrieved 26 February 2015.
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.
- ^ "An overview on dsls". Archived from the original on October 23, 2007.
- ^ "Declarative modelling". Simulistics. Retrieved 15 August 2013.
- ^ Gordon, Michael J. C. (1996). "From LCF to HOL: a short history". Retrieved 2021-10-30.
- ISBN 0-201-71012-9.
- ISBN 0-201-71012-9.
- ^ "Birth of Prolog" (PDF). November 1992.
- ISSN 0004-3702.
- ^ Robert Kowalski Predicate Logic as a Programming Language Memo 70, Department of Artificial Intelligence, University of Edinburgh. 1973. Also in Proceedings IFIP Congress, Stockholm, North Holland Publishing Co., 1974, pp. 569-574.
- ISBN 978-3-031-35253-9.
- ISBN 978-1-4503-0661-4.
- arXiv:2006.16723.
- ISBN 978-0-521-81802-5.
External links
- Frans Coenen. Characteristics of declarative programming languages. 1999.
- Robert Harper.
- Olof Torgersson. A Note on Declarative Programming Paradigms and the Future of Definitional Programming. 1996.