Declarative problem solving
CIE A-Level Computer ScienceΒ· 20 min read
1. Core Differences Between Declarative and Imperative Paradigmsβ β ββββ± 6 min
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
Compare solving "find all even numbers between 1 and 10" using both paradigms
- 1
Imperative approach: explicit step-by-step instructions:
- 2
- Initialize an empty result list
- Iterate from 1 to 10
- For each number, check if remainder when divided by 2 is 0
- Add matching numbers to the result list
- Return the result list
- 3
Declarative approach: problem description only:
- 4
- 5
Define an even number as where . The solution is all that satisfy this rule. The solver finds results without being told how to iterate.
- 6
Both approaches give the same result , but the problem modelling approach is fundamentally different.
2. Constraint Satisfaction: The Declarative Problem Structureβ β β βββ± 8 min
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.
Formulate the 8-queen problem as a declarative CSP
- 1
- Define variables: Place one queen per row on an 8Γ8 chessboard. Let = the column position of the queen in row , for to .
- 2
- Define domains: Each queen can be placed in any column 1 through 8, so:
- 3
- 4
- Define constraints that no two queens can attack each other:
- 5
- 6
The solver uses these constraints to find a valid set of values for all , 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.
3. Advantages, Disadvantages and Use Casesβ β ββββ± 6 min
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
Identify if declarative approach is suitable for: 1) A product sorting program for an online store, 2) A school timetable scheduling system
- 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
- 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.
4. Common Pitfalls
Wrong move:
Describing step-by-step solution instructions when asked for a declarative problem formulation
Why:
Declarative problem solving only requires describing the problem constraints and end goal, not how the solver should reach the solution
Correct move:
State all variables, domains and constraints that any valid solution must satisfy, leaving the solution process to the solver
Wrong move:
Claiming declarative problem solving is always better than imperative for all problem types
Why:
Declarative approaches have significant performance overhead compared to custom optimized imperative solutions for well-understood problems
Correct move:
Match the paradigm to the problem: use declarative for complex constraint problems, imperative for routine algorithms
Wrong move:
Forgetting all three core components of a constraint satisfaction problem
Why:
Without variables, domains and constraints, the problem is not fully defined for a solver
Correct move:
Always explicitly list all three components when asked to formulate a CSP
Wrong move:
Confusing declarative problem solving with declarative programming languages
Why:
While declarative languages like Prolog are often used for declarative solutions, the paradigm is separate from the language used
Correct move:
Remember that declarative problem solving is an approach to modelling problems that can be implemented in any language
5. Quick Reference 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 |
When this came up on past exams
AI-estimated based on syllabus patterns β cross-check with official past papers for accuracy. Use only as revision-focus signals.
- 2022 Β· 11
Compare declarative/imperative approaches
- 2023 Β· 12
Formulate Sudoku as declarative problem
- 2024 Β· 13
State benefits of declarative problem solving
Going deeper
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.
