In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, declarative programming is a
programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a
computation
Computation is any type of arithmetic or non-arithmetic calculation that follows a well-defined model (e.g., an algorithm).
Mechanical or electronic devices (or, historically, people) that perform computations are known as ''computers''. An esp ...
without describing its
control flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
.
Many languages that apply this style attempt to minimize or eliminate
side effects
In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequenc ...
by describing ''what'' the program must accomplish in terms of the
problem domain, rather than describe ''how'' to accomplish it as a sequence of the programming
language primitive
In computing, language primitives are the simplest elements available in a programming language. A primitive is the smallest 'unit of processing' available to a programmer of a given machine, or can be an atomic element of an expression in a la ...
s
(the ''how'' being left up to the language's
implementation). This is in contrast with
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program ...
, which implements
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
s in explicit steps.
Declarative programming often considers
programs
Program, programme, programmer, or programming may refer to:
Business and management
* Program management, the process of managing several related projects
* Time management
* Program, a part of planning
Arts and entertainment Audio
* Progra ...
as theories of a
formal logic, and computations as deductions in that logic space. Declarative programming may greatly simplify writing
parallel programs.
Common declarative languages include those of
database query languages (e.g.,
SQL,
XQuery),
regular expressions,
logic programming,
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions tha ...
, and
configuration management 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
In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequenc ...
(or more specifically, is
referentially transparent
In computer science, referential transparency and referential opacity are properties of parts of computer programs. An expression is called ''referentially transparent'' if it can be replaced with its corresponding value (and vice-versa) with ...
)
* A language with a clear correspondence to
mathematical logic
Mathematical logic is the study of formal logic within mathematics. Major subareas include model theory, proof theory, set theory, and recursion theory. Research in mathematical logic commonly addresses the mathematical properties of forma ...
.
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
Functional may refer to:
* Movements in architecture:
** Functionalism (architecture)
** Form follows function
* Functional group, combination of atoms within molecules
* Medical conditions without currently visible organic basis:
** Functional sy ...
and
logical programming
Logic programming is a programming paradigm which is largely based on formal logic. Any program written in a logic programming language is a set of sentences in logical form, expressing facts and rules about some problem domain. Major logic pr ...
languages are characterized by a declarative programming style. In
logical programming languages, programs consist of logical statements, and the program executes by searching for proofs of the statements.
In a
pure functional language
In computer science, purely functional programming usually designates a programming paradigm—a style of building the structure and elements of computer programs—that treats all computation as the evaluation of mathematical functions.
Progr ...
, 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.
Some logical 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
umbrella term
In linguistics, semantics, general semantics, and ontologies, hyponymy () is a semantic relation between a hyponym denoting a subtype and a hypernym or hyperonym (sometimes called umbrella term or blanket term) denoting a supertype. In othe ...
that includes a number of better-known
programming paradigms.
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
Well-known examples of declarative
domain-specific languages (DSLs) include the
yacc parser generator input language,
QML
QML (Qt Modeling Language) is a user interface markup language. It is a declarative language (similar to CSS and JSON) for designing user interface–centric applications. Inline JavaScript code handles imperative aspects. It is associated wi ...
, the
Make build specification language,
Puppet
A puppet is an object, often resembling a human, animal or mythical figure, that is animated or manipulated by a person called a puppeteer. The puppeteer uses movements of their hands, arms, or control devices such as rods or strings to move ...
's configuration management language,
regular expressions, and a subset of
SQL (SELECT queries, for example). DSLs have the advantage of being useful while not necessarily needing to be
Turing-complete
In computability theory, a system of data-manipulation rules (such as a computer's instruction set, a programming language, or a cellular automaton) is said to be Turing-complete or computationally universal if it can be used to simulate any ...
, which makes it easier for a language to be purely declarative.
Many markup languages such as
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaS ...
,
MXML
MXML is an XML-based user interface markup language first introduced by Macromedia in March 2004. Application developers use MXML in combination with ActionScript to develop rich web applications, with products such as Apache Flex.
Adobe Syst ...
,
XAML,
XSLT or other
user-interface markup language
A user interface markup language is a markup language that renders and describes graphical user interfaces and controls. Many of these markup languages are dialects of XML and are dependent upon a pre-existing scripting language engine, usually a ...
s are often declarative. HTML, for example, only describes what should appear on a webpage - it specifies neither
control flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
for rendering a page nor the page's possible
interactions with a user.
, some software systems 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
Soap is a salt of a fatty acid used in a variety of cleansing and lubricating products. In a domestic setting, soaps are surfactants usually used for washing, bathing, and other types of housekeeping. In industrial settings, soaps are us ...
.
Functional programming
Functional programming languages such as
Haskell,
Scheme A scheme is a systematic plan for the implementation of a certain idea.
Scheme or schemer may refer to:
Arts and entertainment
* ''The Scheme'' (TV series), a BBC Scotland documentary series
* The Scheme (band), an English pop band
* ''The Schem ...
, 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. For example, in
Scheme A scheme is a systematic plan for the implementation of a certain idea.
Scheme or schemer may refer to:
Arts and entertainment
* ''The Scheme'' (TV series), a BBC Scotland documentary series
* The Scheme (band), an English pop band
* ''The Schem ...
, the order of evaluation of many kinds of sub-expressions is undefined or implicit. Instead, computations are characterised by various kinds of recursive
higher-order function
In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
* takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itse ...
application and
composition, and as such can be regarded simply as a set of mappings between
domain
Domain may refer to:
Mathematics
*Domain of a function, the set of input values for which the (total) function is defined
** Domain of definition of a partial function
** Natural domain of a partial function
**Domain of holomorphy of a function
* ...
s and
codomains. Many functional languages, including most of those in the ML and Lisp families, are not
purely functional, and thus allow the introduction of
stateful effects in programs, though this is typically avoided when possible.
Hybrid languages
Makefiles, for example, specify dependencies in a declarative fashion, 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
Logic programming languages such as
Prolog state and query relations. The specifics of ''how'' these queries are answered is up to the implementation and its theorem prover, but typically take the form of some sort of
unification. Like functional programming, many logic programming languages permit side effects, and as a result are not strictly declarative.
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
A modeling language is any artificial language that can be used to express information or knowledge or systems in a structure that is defined by a consistent set of rules. The rules are used for interpretation of the meaning of components in th ...
s and environments include
Analytica,
Modelica
Modelica is an object-oriented, declarative, multi-domain modeling language for component-oriented modeling of complex systems, e.g., systems containing mechanical, electrical, electronic, hydraulic, thermal, control, electric power or process- ...
and
Simile.
Examples
Lisp
Lisp (1958) stands for "LISt Processor."
It is tailored to process
lists
A ''list'' is any set of items in a row. List or lists may also refer to:
People
* List (surname)
Organizations
* List College, an undergraduate division of the Jewish Theological Seminary of America
* SC Germania List, German rugby union ...
. A data structure is formed by building lists of lists. In memory, this forms a
tree data structure
In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be c ...
. Internally, the tree structure of typical Lisp data lends itself nicely to processing with
recursive functions.
The syntax to build a tree is to enclose the whitespace-separated
elements within parenthesis. The following is a list of three elements. The first two elements are themselves lists of two elements each:
((A B) (HELLO WORLD) 94)
Lisp has functions to extract and reconstruct elements.
The function
car
(sometimes called
first
) returns the first element in the list. The function
cdr
(sometimes called
rest
) returns a list containing everything but the first element. The function
cons
returns a list that is the concatenation of two data elements. Therefore, the following expression will return the list
X
:
(cons (car x) (cdr x))
The above code returns a ''copy'' of the list
X
. As is typical in functional languages, operations in Lisp often copy data when asked to produce new data from old. The tree structure of Lisp data facilitates this as well: a new structure made from pre-existing data shares as much of its internal structure with its antecedents as possible, with new additions stored as branches of the tree, and references to the original structure under its original name will return exactly that and no more.
One drawback of Lisp is when many functions are nested, the parentheses may look confusing.
Modern Lisp
environments
Environment most often refers to:
__NOTOC__
* Natural environment, all living and non-living things occurring naturally
* Biophysical environment, the physical and biological factors along with their chemical interactions that affect an organism or ...
help ensure parenthesis match. As an aside, Lisp does support the ''imperative language'' operations of the assignment statement and goto loops.
Also, ''Lisp'' is not concerned with the
datatype of the elements at compile time. Instead, it assigns the datatypes at runtime. This may lead to programming errors not being detected early in the development process. To counteract this, Lisp development is typically carried out in an extremely incremental manner, with functions and higher-order functions built up and tested live during development. Additionally, through the use of
macros, which are Lisp functions that operate upon Lisp programs as data structures, type checking may be performed discretionally at any point the programmer wishes.
Writing large, reliable, and readable Lisp programs requires forethought. If properly planned, the program may be much shorter than an equivalent ''imperative language'' program.
''Lisp'' is widely used in
artificial intelligence
Artificial intelligence (AI) is intelligence—perceiving, synthesizing, and inferring information—demonstrated by machines, as opposed to intelligence displayed by animals and humans. Example tasks in which this is done include speech ...
. However, its usage has been accepted only because it has ''imperative language'' operations, making unintended side-effects possible.
ML
ML (1973)
stands for "Meta Language." ML is statically typed, and function arguments and return types may be annotated.
''ML'' is not parenthesis-eccentric like ''Lisp''. 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.
Prolog
Prolog (1972) stands for "PROgramming in LOGic." It was designed to process
natural languages.
The building blocks of a Prolog program are ''
objects
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ai ...
'' and their ''relationships'' to other objects. ''Objects'' are built by stating true ''facts'' about them.
Set theory
Set theory is the branch of mathematical logic that studies sets, which can be informally described as collections of objects. Although objects of any kind can be collected into a set, set theory, as a branch of mathematics, is mostly concern ...
''facts'' are formed by assigning objects to sets. The syntax is
setName(object).
* Cat is an animal.
:
animal(cat).
* Mouse is an animal.
:
animal(mouse).
* Tom is a cat.
:
cat(tom).
* Jerry is a mouse.
:
mouse(jerry).
Adjective
In linguistics, an adjective ( abbreviated ) is a word that generally modifies a noun or noun phrase or describes its referent. Its semantic role is to change information given by the noun.
Traditionally, adjectives were considered one of the ...
''facts'' are formed using
adjective(object).
* Cat is big.
:
big(cat).
* Mouse is small.
:
small(mouse).
''Relationships'' are formed using multiple items inside the parentheses. In our example we have
verb(object,object)
and
verb(adjective,adjective)
.
* Mouse eats cheese.
:
eat(mouse,cheese).
* Big animals eat small animals.
:
eat(big,small).
After all the facts and relationships are entered, then a question can be asked:
: Will Tom eat Jerry?
:
?- eat(tom,jerry).
Prolog's usage has expanded to become a goal-oriented language.
In a goal-oriented application, the goal is defined by providing a list of subgoals. Then each subgoal is defined by further providing a list of its subgoals, etc. If a path of subgoals fails to find a solution, then that subgoal is
backtracked and another path is systematically attempted.
Practical applications include solving the
shortest path problem
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between ...
and producing
family tree
A family tree, also called a genealogy or a pedigree chart, is a chart representing family relationships in a conventional tree structure. More detailed family trees, used in medicine and social work, are known as genograms.
Representations ...
s.
See also
*
Comparison of programming paradigms
*
Inductive programming
Inductive programming (IP) is a special area of automatic programming, covering research from artificial intelligence and programming, which addresses learning of typically declarative ( logic or functional) and often recursive programs from in ...
*
List of declarative programming languages
References
External links
* Frans Coenen
Characteristics of declarative programming languages 1999.
*
Robert Harper.
What, If Anything, Is A Declarative Language? 2013.
There Is Such A Thing As A Declarative Language, and It’s The World’s Best DSL 2013.
* Olof Torgersson
1996.
{{Authority control
Programming paradigms