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

20.1 Programming Paradigms – Declarative

1. Definition (AO1)

  • A declarative paradigm describes what the program must achieve, leaving the how to the language implementation (compiler, interpreter or runtime).
  • The programmer supplies a high‑level specification – e.g. a query, a set of facts/rules, or a pure function – and the system determines the execution steps, optimisation and ordering.
  • Control flow, state handling and optimisation are implicit rather than explicit.

2. Key Characteristics

CharacteristicWhat it means for the programmer
Specification of logic (AO1)Code expresses relationships, constraints or transformations instead of step‑by‑step commands.
Implicit control flow (AO1)Loops, recursion, back‑tracking, or lazy evaluation are performed automatically by the language system.
Immutability & pure functions (AO1)Values cannot be altered after creation; a function always returns the same result for the same arguments.
Higher‑level abstraction (AO2)Programs are shorter, more readable and map closely to the problem domain.
Evaluation strategies (AO1)Typical strategies are lazy (demand‑driven) evaluation, eager (strict) evaluation and back‑tracking search.

3. Evaluation Strategies Explained (AO1)

  • Lazy (deferred) evaluation – an expression is evaluated only when its value is required.

    -- Haskell

    evens = filter even [1..] -- infinite list, not evaluated yet

    firstFive = take 5 evens -- only the first five elements are computed

  • Eager (strict) evaluation – expressions are evaluated as soon as they are bound to a name. Most imperative languages behave this way.
  • Back‑tracking – the interpreter explores alternatives, abandons a dead‑end and returns to the previous choice point.

    % Prolog

    grandparent(X,Z) :- parent(X,Y), parent(Y,Z).

4. Typical Declarative Languages

LanguageParadigm typeTypical use in the syllabus
SQLDeclarative query language (set‑based)Database querying, data manipulation (AO1, AO2)
PrologLogic programmingKnowledge representation, rule‑based inference (AO1, AO2)
Haskell / F# / ErlangFunctional programmingPure functions, higher‑order functions, immutability (AO1, AO2)
MiniZinc, CLPConstraint programmingModelling optimisation and satisfaction problems (AO1, AO2)
HTML / CSSMarkup & style (declarative description of structure & presentation)Web page design – not examined for code writing but useful for understanding declarative description (AO1)
Jess / DroolsRule‑based AIExpert‑system style reasoning (AO1, AO2)

5. Domains of Use (AO1)

  • Relational database queries (SQL)
  • Logical inference and expert systems (Prolog, rule‑based AI)
  • Constraint satisfaction & optimisation (MiniZinc, CLP)
  • Web page structure & styling (HTML, CSS)
  • Concurrent, fault‑tolerant applications (Erlang)
  • Mathematical modelling and symbolic computation (functional languages)

6. Advantages (AO2) and Limitations (AO3)

AdvantagesLimitations

  • Higher abstraction – easier to reason about *what* the program does.
  • Automatic optimisation (re‑ordering, parallelisation, pruning).
  • Reduced bugs from mutable state; easier reasoning about correctness.
  • Concise code – often fewer lines than an equivalent imperative solution.
  • Natural fit for queries, constraints, and logical inference.

  • Performance can be unpredictable; hidden evaluation may be slower for tight loops.
  • Hard to guarantee real‑time response or hard deadlines.
  • Limited low‑level hardware access (device drivers, direct memory manipulation).
  • Steeper learning curve for students used to step‑by‑step thinking.
  • Debugging can be difficult because control flow is hidden.

7. Exam‑Relevant Skills (Cambridge 9618 – AO1‑AO3)

  1. Write a simple declarative statement (AO1)

    • SQL: SELECT name FROM Students WHERE grade > 80;
    • Prolog: ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
    • Haskell: sumSquares xs = sum (map (^2) xs)

  2. Identify the paradigm from a code fragment (AO1)

    • Fragment containing SELECT … FROM … WHERE … → Declarative (SQL).
    • Fragment with facts, rules and the operator :- → Declarative (Logic programming).
    • Fragment defining only pure functions, using higher‑order functions → Declarative (Functional).

  3. Justify the choice of a declarative approach for a given problem (AO2)

    • Explain why a database query is best expressed in SQL (set‑based, optimiser handles indexing).
    • Explain why a scheduling problem fits MiniZinc (constraints are natural, solver finds a feasible schedule).

  4. Compare declarative and imperative solutions (AO2‑AO3)

    • State the benefits (conciseness, automatic optimisation) and drawbacks (loss of fine‑grained control) for the specific scenario.
    • Discuss how the choice may affect correctness, performance and ease of debugging.

8. Sample Exam‑Style Code Fragments & Marking Points

8.1 SQL – “Students scoring > 80 in Mathematics”

SELECT name

FROM Students

WHERE subject = 'Mathematics' AND score > 80;

Marking guide (AO1‑AO2): identify the language (SQL), describe what the query does, and note that the DBMS decides the access path (indexes, joins).

8.2 Prolog – Grandparent Relationship

grandparent(X,Z) :- parent(X,Y), parent(Y,Z).

Marking guide: recognise the rule syntax, explain back‑tracking through parent facts, and state that the interpreter searches for bindings that satisfy both sub‑goals.

8.3 Haskell – Lazy Generation of Even Numbers

evens      = filter even [1..]   -- infinite list, not evaluated yet

firstFive = take 5 evens -- only the first five elements are forced

Marking guide: identify lazy evaluation, describe how only the needed part of the infinite list is computed.

8.4 MiniZinc – Simple Scheduling Constraint

int: n = 3;                       % three tasks

array[1..n] of var 1..n: start; % start time of each task

constraint forall(i in 1..n-1) (start[i] < start[i+1]); % tasks must be ordered

solve satisfy;

Marking guide: recognise the constraint model, explain that the solver searches for values of start satisfying the ordering constraint.

9. Comparison with Imperative Programming (AO2)

AspectDeclarativeImperative
FocusWhat the result should beHow to compute the result
Control flowImplicit (handled by language runtime)Explicit (loops, conditionals)
StateImmutable, pure functions where possibleMutable variables and side‑effects common
Typical problemsDatabase queries, logical inference, constraint solvingAlgorithmic loops, low‑level I/O, real‑time control
DebuggingHarder to trace hidden execution orderStraightforward step‑by‑step tracing

10. Quick Revision Checklist (AO1‑AO3)

  • Can you define “declarative programming” and list its core characteristics?
  • Do you know the evaluation strategies (lazy, eager, back‑tracking) and can you give a short example?
  • Can you name at least three declarative languages and the domains they are used for?
  • Are you able to write a simple SQL query, a Prolog rule, and a pure functional expression?
  • Can you explain why a problem is better solved declaratively than imperatively?
  • Do you understand the main advantages and the situations where declarative approaches are unsuitable?

11. Summary (AO1‑AO2)

The declarative paradigm shifts the programmer’s role from giving explicit step‑by‑step instructions to specifying the desired outcome. By abstracting control flow, state management and optimisation, declarative languages provide concise, readable solutions for domains such as database querying, logical inference, constraint solving and functional computation. Mastery of the characteristics, typical languages, evaluation strategies, advantages, limitations and exam‑style tasks enables students to select the most appropriate paradigm for a given problem and to answer Cambridge AS & A Level Computer Science (9618) questions with confidence.

Suggested diagram for revision book: “Flow of a declarative query” – user writes a high‑level specification → parser → optimiser → execution engine → result.