32.1 Paradigms & the Declarative Idea
Up to now, almost every program you have written has been a list of instructions: do this, then do that, loop until something is true. You told the computer exactly how to reach the answer, one step at a time. That style is called imperative (or procedural) programming, and it is what Python, Java and VB.NET are built for.
Declarative programming turns this on its head. Instead of writing steps, you describe what is true in your problem — a set of plain statements about your world — and then you ask questions. The computer works out the steps for you. You never write a loop, you never manage a counter, you simply state the facts and let the system search them.
By the end of this page you will be able to write facts to record information, write a goal to ask a question and state exactly what it returns, and write a rule that combines facts to define a new relationship.
| Term | Meaning |
|---|---|
| Programming paradigm | A style or approach to writing programs. The same problem can be solved in several styles. |
| Imperative (procedural) | A paradigm where you write the exact steps (the "how"), using variables, assignment and loops in a set order. |
| Declarative | A paradigm where you state what is true (the "what") using facts and rules, then ask questions. The system finds the "how". |
| Fact | A single piece of information stated as always true, e.g. that a particular student knows Python. |
| Predicate | The name of a fact or relationship, written before the brackets, e.g. knows in knows(aymaan, python). |
| Argument | A value inside the brackets of a fact, e.g. aymaan and python. Written in lower case. |
| Variable | A placeholder in a goal or rule that can stand for any value. Written with a Capital letter, e.g. X or Student. |
| Goal (query) | A question asked of the knowledge base. The system tries to "satisfy" it by finding matching facts. |
| Rule | A statement that is true only IF other conditions are true. It lets you define new relationships from existing facts. |
| Knowledge base | The complete collection of facts and rules that the system searches to answer goals. |
- Loops, assignment (← or =) and variables changing → imperative.
- Lower-case statements ending in a full stop like
parent(john, ethel).→ declarative. - Classes, methods, inheritance → object-oriented.
- LDD / STO / INC mnemonics → low-level.
- Step 1 — Core idea: states what, not how.
- Step 2 — Mechanism: uses facts and rules, and queries to satisfy goals.
- Think about variables, assignment, and the order statements run in. Two distinct ideas earn two marks.
- For “describe the paradigm” questions, one short sentence is not enough for two marks — you must give two separate points.
- For declarative, the safest pair is “states what, not how” + “uses facts and rules / goals”.
- Do not waste time naming a specific language like Prolog; it earns nothing.
32.1 Paradigms & the Declarative Idea
32.2 Facts
- Facts are the foundation of everything else — goals search them and rules combine them.
- Cambridge almost always opens a declarative question by giving you a knowledge base and a sentence of new information, then asking you to “write additional clauses”.
- Mark scheme: In “write additional clauses” questions, one mark is awarded for each correctly written clause.
- A sentence carrying three pieces of information needs three separate facts.
- Lower-case names, the right predicate, and the correct argument order all matter — a fact with the arguments swapped scores zero.
A fact records one piece of information that is simply true. In 9618 a fact is written as a predicate (the relationship name) followed by one or more arguments in brackets, ending in a full stop:
knows(aymaan, python).
// predicate arguments full stop
// Read: "aymaan knows python"Here is a small knowledge base for the coding club at Glenrich:
language(python).
language(java).
language(vbnet).
language(pseudocode).
student(musarrat).
student(tarnima).
student(aymaan).
student(mehrin).
student(naib).
knows(musarrat, python).
knows(tarnima, java).
knows(aymaan, python).
knows(mehrin, vbnet).
knows(naib, python).- Facts use lower-case for predicate names and arguments.
- A Capitalised word means a variable, which belongs in goals and rules, never in a fact.
- Argument order carries meaning:
mentors(musarrat, naib)(“Musarrat mentors Naib”) is not the same asmentors(naib, musarrat).
- Step 1 — Break the sentence into separate facts (3 pieces: member, knows, struggles).
- Step 2 — Match each to an existing predicate (student, knows, struggles).
- Step 3 — Write each as a lower-case clause ending in a full stop.
- Three facts again. “Mentors” takes the mentor first, then the person being mentored.
- The most common lost mark here is argument order.
- If the sentence says “Nazifa mentors Naib”, the mentor goes first.
- Re-read the example clause meaning given in the question and copy that order exactly.
- Borrower first, book title second — copy the order of the given example exactly.
- Copy the order of the given example exactly.
32.2 Facts
32.3 Goals & Queries
- A knowledge base is useless until you ask it something.
- A goal is the question.
- Cambridge shows you what one goal returns, then asks you to write what a different goal returns — a quick one-mark question you can only get right if you understand exactly how matching works.
- Mark scheme: The mark is given for listing all matching values and only the matching values.
- Order is not usually penalised, but a missing match or an extra wrong one loses the mark.
- The answer is written as
Variable = value1, value2, ...
A goal is asked using the same shape as a fact, but with a variable (a Capitalised name) in the slot you want answers for. The system looks through every fact, and wherever the goal matches, the variable is set to that value. This matching is called instantiation.
? knows(X, python)
// The system checks each knows fact and keeps those
// whose second argument is python. Three match:
// knows(musarrat, python)
// knows(aymaan, python)
// knows(naib, python)
// So:
X = musarrat, aymaan, naib- A goal with a variable returns every value that satisfies it.
- Scan the facts one by one, keep the matches, list them all.
- If no fact matches, the goal returns nothing (it fails).
- Putting a variable in a different position changes the question:
knows(naib, L)asks “which languages does Naib know?”, givingL = python.
- Find facts with second argument java. Only one matches.
- This goal matches every student fact. List them all.
- Two mistakes lose this easy mark: missing a match (read every fact, not just the first you spot) and matching the wrong position.
- Cover the column you are not searching with your finger so you only compare the right argument.
32.3 Goals & Queries
32.4 Rules
- Facts only store information you already have.
- A rule lets the system work out new information — relationships that are never written down directly.
- This is the highest-value declarative question on Paper 3 (usually 4 marks), and it is the one students most often get wrong.
- Mark scheme: A 4-mark rule is marked as one mark per condition (each predicate translated correctly, with the variables in the right place) plus one mark for all the Boolean operators and punctuation being correct, with no extra lines of code.
- A comma is accepted in place of AND.
A rule has a head (the new relationship being defined) and a body (the conditions that must hold). They are joined by IF, and conditions are combined with AND, OR and NOT:
head(Args) IF condition1 AND condition2 AND NOT condition3Suppose the club wants a rule for who can help with a language. A member S can help with language L if they know L and they do not struggle with L:
can_help(S, L) IF knows(S, L) AND NOT struggles(S, L)- Translate the English phrase by phrase: “is a / knows / has done X” becomes a positive condition; “does not / has not” becomes
NOT; “and” becomesAND; “or” becomesOR. - Re-use the same variable names from the head inside the body so the conditions connect.
- Step 1 — "knows L": knows(S, L).
- Step 2 — "mentors another member": use a fresh variable M: mentors(S, M).
- Step 3 — Join with AND.
- Step 4 — Check punctuation and no extra lines.
- Three conditions: language(L), struggles(S, L), and NOT mentors(M, S), all joined with AND.
- The mark for “operators and punctuation” is lost if you add extra code the question did not ask for, or if you forget a
NOTon a “does not” phrase. - Underline every “not”, “and”, “or” in the question stem before you write — each maps to exactly one operator.
- language(L) AND NOT knows(S, L).
- Keep it to the conditions you are sure of: knows(S, python) AND knows(S, L) AND language(L).
32.4 Rules
32.5 Building a Knowledge Base — Full Exam-Style Question
- The full Paper 3 question puts everything together in one scenario: add facts, state what a goal returns, and write a rule.
- This closing section gives you one complete exam-style question so you can rehearse the whole sequence.
When you design a small knowledge base from scratch, work in this order: first decide your predicates (the kinds of fact you need), then write the facts, then think about which relationships are not stored directly and write rules for them.
- A declarative programming language is used to record information about a hospital ward at a clinic in Dhaka.
- The knowledge base records the nurses, the wards, which nurse is assigned to which ward, and which nurse is trained in a particular skill.
01 nurse(musarrat).
02 nurse(tarnima).
03 nurse(aymaan).
04 nurse(mehrin).
05 nurse(naib).
06
07 ward(emergency).
08 ward(children).
09 ward(surgery).
10
11 assigned(musarrat, emergency).
12 assigned(tarnima, children).
13 assigned(aymaan, emergency).
14 assigned(mehrin, surgery).
15
16 trained(musarrat, paediatrics).
17 trained(aymaan, trauma).
18 trained(naib, trauma).
19 unavailable(mehrin).Clause meanings: 01: Musarrat is a nurse. 07: Emergency is a ward. 11: Musarrat is assigned to the emergency ward. 16: Musarrat is trained in paediatrics. 19: Mehrin is unavailable.
(a) [4] Nawal is a nurse who is assigned to the surgery ward and is trained in trauma. Write the additional clauses to represent this information. (AO2)
(b)(i) [1] Using the variable N, the goal assigned(N, emergency) returns N = musarrat, aymaan. Write the result returned by the goal trained(N, trauma). (AO2)
(b)(ii) [2] Write the result returned by the goal ward(W), and explain in one sentence why assigned(naib, W) returns nothing. (AO2)
(c) [4] A nurse N can cover ward W if N is trained in trauma, N is not already assigned to W, and N is not unavailable. Write this as a rule for can_cover(N, W). (AO3)
- (a) 4 clauses: nurse(nawal). assigned(nawal, surgery). trained(nawal, trauma). + lower-case/full stops/no extra. (ward(surgery) already exists.)
- (b)(i) N = aymaan, naib.
- (b)(ii) W = emergency, children, surgery; no assigned(naib, ...) fact exists.
- (c) can_cover(N, W) IF trained(N, trauma) AND NOT assigned(N, W) AND NOT unavailable(N).
32.5 Building a Knowledge Base
✓ Key Points Summary
32.6 Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked solution.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
Declarative programming describes:
Question 2Multiple Choice
Imperative (procedural) programming describes:
Question 3True / False
Facts use lower-case for predicate names and arguments.
Question 4Multiple Choice
What is a fact in declarative programming?
Question 5Multiple Choice
What is a goal (query)?
Question 6Multiple Choice
How is a variable written in a goal?
Question 7Multiple Choice
A goal with a variable returns:
Question 8True / False
A rule has a head (new relationship) and a body (conditions), joined by IF.
Question 9Multiple Choice
How is "does not" translated in a rule?
Question 10Multiple Choice
Argument order in facts:
Question 11Multiple Choice
What is a knowledge base?
Question 12Multiple Choice
Which code fragment is declarative?
Answer all 12 questions to enable submission.