ArticleslgStudy

science

Declarative programming

Declarative programming is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Declarative programming rather than just read about it. In short: In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without fully describing its control flow. The paradigm focuses on abstracting a program beyond the order of execution.

Key takeaways

  • Declarative programming belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Declarative programming to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Declarative programming from memory before moving on to harder problems.

Reference excerpt

In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without fully describing its control flow. 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:

Any programming language that lacks side effects, or more specifically, has referential transparency. A language with a clear correspondence to mathematical logic.

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.

Examples

Prolog Prolog (1972) stands for "PROgramming in LOGic." It was developed for natural language question answering, using SL resolution 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:

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. The broad range of Prolog applications is highlighted in the Year of Prolog Book, 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. 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. Datalog has been applied to such problems as data integration, information extraction, networking, security, cloud computing and machine learning.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Declarative programming

Start with the simplest possible case. Write down what Declarative programming claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Declarative programming before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Declarative programming ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Declarative programming

In research
Declarative programming appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Declarative programming in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Declarative programming is common in secondary-school and first-year university syllabi. It links to neighbouring topics Declarative programming, Programming paradigms, so understanding it makes those chapters shorter.
In everyday life
Look for Declarative programming outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Declarative programming” →

How to study Declarative programming in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Declarative programming means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Declarative programming out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Declarative programming in simple terms?

In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without fully describing its control flow. The paradigm focuses on abstracting a program beyond the order of execution.

Why does Declarative programming matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Declarative programming?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Declarative programming.

Tags

  • Declarative programming
  • Programming paradigms

Keep exploring