Lably
Skip to content

Declarative programming

Programming paradigm based on modeling the logic of a computation

  • 6 min read
  • 2 chapters

Related on Lably

Through Computer science, Logic and Computation

The paradigm focuses on abstracting a program beyond the order of execution. This implies usage of a high-level programming language.

Languages that permit this style allow a developer to minimize or eliminate side effects by describing what the program must accomplish in terms of the problem domain, rather than fully describing how to accomplish it as a sequence of the programming language primitives (the how being left up to the language's implementation). Declarative programming is in contrast with imperative programming, which implements algorithms in explicit steps.

Just like in the functional programming paradigm, mutating variables does not conform to the declarative paradigm because it implies an execution order which is imperative.

Declarative programming may consider programs as theories of a formal logic, and computations as deductions in that logical theory. Declarative programming at times simplifies the writing of parallel programs.

Common declarative language paradigms include logic programming (e.g., Prolog, Datalog, answer set programming), and algebraic modeling systems.

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.

Other definitions:

Subparadigms

Declarative programming is an umbrella term that includes a number of better-known programming paradigms.

Functional Programming

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.

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.

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 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.

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

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.

Sign in to keep reading

One more chapter is free with a Lably account.

More in Computing and communication