# Declarative problem solving

> CIE A-Level Computer Science · 9618
> Source: https://www.owlsprep.com/study/cie-9618-u13-declarative-problem-solving/

This module introduces the declarative problem solving paradigm, which focuses on describing problem facts and constraints rather than coding step-by-step solutions. You will learn its core features, differences from imperative approaches, and common use cases.

**Prerequisites:** [Introduction to computational thinking](https://www.owlsprep.com/study/cie-9618-u13-introduction-to-computational-thinking/)

## Learning objectives

- Distinguish between declarative and imperative problem solving approaches
- Formulate simple problems as declarative constraint satisfaction problems
- Evaluate the suitability of declarative approaches for different problem types
- Identify key advantages and limitations of declarative problem solving

## Core Differences Between Declarative and Imperative Paradigms

**Declarative Problem Solving** — A problem solving paradigm where you define the problem facts, desired end state, and any rules/constraints that must be satisfied. A general solver derives the solution, rather than you writing explicit steps.

*Example:* Describing the rules of a puzzle instead of coding how to solve it

In contrast, imperative problem solving requires you to explicitly define every step to transform an input into the desired output. Every decision and operation is ordered by the problem solver. Declarative problem solving shifts the work of finding the solution path from the human problem solver to a general-purpose solver.

- Declarative answers *what* the problem is; imperative answers *how* to solve it
- Declarative changes only require updating facts/rules; imperative require changing step order
- Declarative solvers are general purpose; imperative solutions are problem-specific

**Worked example:** Compare solving "find all even numbers between 1 and 10" using both paradigms

1. Imperative approach: explicit step-by-step instructions:
2. 1. Initialize an empty result list
2. Iterate from 1 to 10
3. For each number, check if remainder when divided by 2 is 0
4. Add matching numbers to the result list
5. Return the result list
3. Declarative approach: problem description only:
4. $$A = \{1, 2, 3, 4, 5, 6, 7, 8, 9, 10\}$$
5. Define an even number as $n$ where $n \mod 2 = 0$. The solution is all $n \in A$ that satisfy this rule. The solver finds results without being told how to iterate.
6. Both approaches give the same result $[2,4,6,8]$, but the problem modelling approach is fundamentally different.

## Constraint Satisfaction: The Declarative Problem Structure

**Constraint Satisfaction Problem (CSP)** — A standard declarative problem structure made of three core components: a set of variables, a domain of possible values for each variable, and a set of constraints that valid solutions must satisfy.

*Example:* Sudoku: each cell is a variable, domain 1-9, constraints are no repeats in rows/columns/boxes

Most complex real-world problems suited to declarative solving can be formulated as CSPs. Once the problem is correctly defined, a general CSP solver can find a valid solution or confirm no solution exists, with no custom solving code required.

**Worked example:** Formulate the 8-queen problem as a declarative CSP

1. 1. Define variables: Place one queen per row on an 8×8 chessboard. Let $Q_i$ = the column position of the queen in row $i$, for $i = 1$ to $8$.
2. 2. Define domains: Each queen can be placed in any column 1 through 8, so:
3. $$\text{dom}(Q_i) = \{1, 2, 3, 4, 5, 6, 7, 8\} \quad \forall i$$
4. 3. Define constraints that no two queens can attack each other:
5. $$Q_i \neq Q_j \quad \forall i \neq j \text{ (no shared column)} \\ |Q_i - Q_j| \neq |i - j| \quad \forall i \neq j \text{ (no shared diagonal)}$$
6. The solver uses these constraints to find a valid set of values for all $Q_i$, no need to specify backtracking or search steps.

> **Exam tip:** When asked to formulate a CSP in an exam, you only need to describe the constraints, not how to check them.

## Advantages, Disadvantages and Use Cases

Declarative problem solving is not suitable for every problem. It has clear strengths for certain problem types, but significant limitations compared to imperative approaches for others.

- **Advantages**: Easier to model complex problems with many constraints, faster development time, easier debugging (only facts/rules need checking), general solvers are reusable
- **Disadvantages**: Less efficient than custom imperative solutions, solver performance depends on problem complexity, harder to optimize for edge cases

**Worked example:** Identify if declarative approach is suitable for: 1) A product sorting program for an online store, 2) A school timetable scheduling system

1. 1. Sorting program: Sorting is a well-understood process with optimized step-by-step algorithms. A declarative approach (only describing that output must be sorted) would be much less efficient than a custom imperative algorithm. Declarative is not suitable here.
2. 2. School timetabling: Timetabling has dozens of fixed constraints: no teacher in two places at once, no overlapping classes for student groups, room capacity limits. It is far easier to describe these constraints declaratively than to write custom scheduling code. Declarative is highly suitable here.
3. Conclusion: Declarative approaches work best for problems with many well-defined constraints where the solution path is not trivial to code manually.

## Common pitfalls

- **Wrong:** Describing step-by-step solution instructions when asked for a declarative problem formulation
  - Why it fails: Declarative problem solving only requires describing the problem constraints and end goal, not how the solver should reach the solution
  - Correct: State all variables, domains and constraints that any valid solution must satisfy, leaving the solution process to the solver
- **Wrong:** Claiming declarative problem solving is always better than imperative for all problem types
  - Why it fails: Declarative approaches have significant performance overhead compared to custom optimized imperative solutions for well-understood problems
  - Correct: Match the paradigm to the problem: use declarative for complex constraint problems, imperative for routine algorithms
- **Wrong:** Forgetting all three core components of a constraint satisfaction problem
  - Why it fails: Without variables, domains and constraints, the problem is not fully defined for a solver
  - Correct: Always explicitly list all three components when asked to formulate a CSP
- **Wrong:** Confusing declarative problem solving with declarative programming languages
  - Why it fails: While declarative languages like Prolog are often used for declarative solutions, the paradigm is separate from the language used
  - Correct: Remember that declarative problem solving is an approach to modelling problems that can be implemented in any language

## Cheatsheet

| Concept | Core Description | Key Feature |
| --- | --- | --- |
| Declarative problem solving | Describes what the problem is | Focuses on facts/constraints, not steps |
| Imperative problem solving | Describes how to solve the problem | Explicit step-by-step instructions |
| Constraint Satisfaction Problem | Standard declarative problem structure | Made of variables, domains, constraints |
| Best use declarative | Complex problems with many constraints | Faster development, reusable solvers |
| Best use imperative | Well-understood routine algorithms | Higher performance, custom optimization |

## What's next

Declarative problem solving is a core computational thinking paradigm that underpins many modern AI and knowledge representation techniques. It provides a flexible way to model complex real-world problems that are difficult to code step-by-step, from scheduling to puzzle solving to route planning. Understanding the difference between declarative and imperative approaches helps you select the right tool for any problem you encounter in the CIE 9618 exam and in practical software development. This foundation prepares you to explore more specialized problem solving paradigms covered later in this unit.

- [Object-Oriented Problem Solving](https://www.owlsprep.com/study/cie-9618-u13-object-oriented-problem-solving/)
- [Artificial intelligence fundamentals](https://www.owlsprep.com/study/cie-9618-u13-artificial-intelligence-fundamentals/)
- [AI search techniques](https://www.owlsprep.com/study/cie-9618-u13-ai-search-techniques/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u13-declarative-problem-solving/
