Show understanding of the characteristics of a number of programming paradigms: Declarative

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Programming Paradigms: Declarative

20.1 Programming Paradigms – Declarative

What is a Declarative Paradigm?

A declarative programming paradigm focuses on what the program should accomplish rather than how to accomplish it. The programmer describes the desired results, and the underlying execution engine determines the steps needed to achieve those results.

Key Characteristics

  • Specification of logic without explicit control flow.
  • Emphasis on expressions, relations, and constraints.
  • Programs are often shorter and more readable for certain problem domains.
  • Side‑effects are minimised; many declarative languages are pure (no mutable state).
  • Evaluation strategy is handled by the language runtime (e.g., lazy evaluation, backtracking).

Common Declarative Languages and Domains

  • SQL – Queries describe the data to retrieve, not the algorithm to scan tables.
  • Prolog – Logic programming where facts and rules define relationships; the interpreter performs backtracking to find solutions.
  • Haskell, Erlang, F# – Functional languages that treat computation as evaluation of mathematical functions.
  • HTML / CSS – Markup and style languages that declare structure and appearance.

Declarative vs Imperative (Comparison Table)

AspectDeclarativeImperative
FocusWhat the result should beHow to achieve the result
Control FlowImplicit, handled by the language runtimeExplicit (loops, conditionals, assignments)
State ChangesMinimised or absent (pure functions, immutable data)Frequent (variables are updated)
Typical Use‑CasesDatabase queries, constraint solving, data transformation, parallel processingSystem programming, algorithms requiring fine‑grained control
Example SyntaxSELECT name FROM students WHERE grade > 80;for(i=0;i<n;i++) total += a[i];

Illustrative Example – SQL Query

Retrieve the names of all students who scored more than 80 in Mathematics:

\$\text{SELECT name FROM students WHERE subject = 'Mathematics' AND score > 80;}\$

The query states the desired result; the database engine decides how to scan indexes, join tables, etc.

Illustrative Example – Prolog Rule

Define a rule for a grandparent relationship:

\$\text{grandparent(X, Z) :- parent(X, Y), parent(Y, Z).}\$

Prolog searches for bindings of X, Y, and Z that satisfy both parent facts.

Advantages of Declarative Programming

  1. Higher level of abstraction – easier to reason about what the program does.
  2. Potential for automatic optimisation by the compiler or interpreter.
  3. Reduced likelihood of bugs related to mutable state.
  4. Often more concise, leading to faster development for suitable problems.

Limitations and When Not to Use

  • Performance can be unpredictable; the hidden execution strategy may be less efficient for certain tasks.
  • Not well suited for low‑level hardware interaction or real‑time constraints.
  • Steeper learning curve for developers accustomed to imperative thinking.

Suggested diagram: Flow of a declarative query – user writes a high‑level specification, the engine parses, optimises, and executes, returning results without explicit loops.

Summary

The declarative paradigm shifts the programmer’s role from directing step‑by‑step procedures to specifying desired outcomes. By abstracting control flow and state management, it enables clearer expression of logic in domains such as databases, logic programming, and functional computation. Understanding its characteristics helps students choose the most appropriate paradigm for a given problem.