Further Programming

Declarative Programming

Describing what to solve rather than how to solve it — the declarative paradigm states facts and rules, then asks questions (goals). The system searches the knowledge base to find the answer. Covers paradigms (imperative vs declarative), facts (lower-case predicates, argument order), goals & queries (variables, instantiation), rules (head + body, IF/AND/OR/NOT), building a knowledge base, and a full exam-style hospital ward question.

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.

TermMeaning
Programming paradigmA 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.
DeclarativeA paradigm where you state what is true (the "what") using facts and rules, then ask questions. The system finds the "how".
FactA single piece of information stated as always true, e.g. that a particular student knows Python.
PredicateThe name of a fact or relationship, written before the brackets, e.g. knows in knows(aymaan, python).
ArgumentA value inside the brackets of a fact, e.g. aymaan and python. Written in lower case.
VariableA 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.
RuleA statement that is true only IF other conditions are true. It lets you define new relationships from existing facts.
Knowledge baseThe complete collection of facts and rules that the system searches to answer goals.

Key rule — telling paradigms apart
  • 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.
Task — Worked Example — Describe declarative [2 marks]
Describe what is meant by a declarative programming language. [2]
Hint:
  • Step 1 — Core idea: states what, not how.
  • Step 2 — Mechanism: uses facts and rules, and queries to satisfy goals.
Your Turn — Your Turn — Describe imperative [2 marks]
Describe what is meant by an imperative (procedural) programming language. [2]
Hint:
  • Think about variables, assignment, and the order statements run in. Two distinct ideas earn two marks.
Exam tip:
  • 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.2 Facts

Why are we doing this?
  • 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”.
Exam tip:
  • 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).
Key rule
  • 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 as mentors(naib, musarrat).
Task — Worked Example — Write additional clauses [3 marks]
Mohar is a new club member who knows pseudocode and struggles with Java. Write the additional clauses to represent this information. [3]
Hint:
  • 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.
Your Turn — Your Turn — Write additional clauses [3 marks]
Nazifa is a new club member who knows Java and mentors Naib. Write the additional clauses to represent this information. [3]
Hint:
  • Three facts again. “Mentors” takes the mentor first, then the person being mentored.
Exam tip:
  • 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.
Task — Worked Example 20.1.2B — Library fact
In a library system, "Mehrin borrowed the book titled atlas." Write this as a fact, given the meaning of borrowed(naib, novel) is "Naib borrowed the book titled novel".
Hint:
  • Borrower first, book title second — copy the order of the given example exactly.
Your Turn — Your Turn 20.1.2B — Library fact
In the same library system, "Aymaan borrowed the book titled poems." Write this as a fact.
Hint:
  • Copy the order of the given example exactly.

32.3 Goals & Queries

Why are we doing this?
  • 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.
Exam tip:
  • 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
Key rule
  • 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?”, giving L = python.
Task — Worked Example — What does knows(X, java) return? [1 marks]
Using the coding-club knowledge base, the goal knows(X, python) returns X = musarrat, aymaan, naib. Write the result returned by the goal knows(X, java). [1]
Hint:
  • Find facts with second argument java. Only one matches.
Your Turn — Your Turn — What does student(X) return? [1 marks]
Using the same knowledge base, write the result returned by the goal student(X). [1]
Hint:
  • This goal matches every student fact. List them all.
Exam tip:
  • 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.4 Rules

Why are we doing this?
  • 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.
Exam tip:
  • 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 condition3

Suppose 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)
Key rule
  • Translate the English phrase by phrase: “is a / knows / has done X” becomes a positive condition; “does not / has not” becomes NOT; “and” becomes AND; “or” becomes OR.
  • Re-use the same variable names from the head inside the body so the conditions connect.
Task — Worked Example — Write a specialist rule [4 marks]
A member S is a specialist in language L if S knows L and S mentors at least one other member. Write this as a rule. [4]
Hint:
  • 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.
Your Turn — Your Turn — Write a should_revise rule [4 marks]
A member S should revise language L if L is a language and S struggles with L and S does not have a mentor for it. Treat "has a mentor" simply as mentors(M, S). Write this as a rule. [4]
Hint:
  • Three conditions: language(L), struggles(S, L), and NOT mentors(M, S), all joined with AND.
Exam tip:
  • The mark for “operators and punctuation” is lost if you add extra code the question did not ask for, or if you forget a NOT on a “does not” phrase.
  • Underline every “not”, “and”, “or” in the question stem before you write — each maps to exactly one operator.
Task — Worked Example 20.1.4B — Write a beginner rule
Write a rule: a member S is a beginner in L if L is a language and S does not know L.
Hint:
  • language(L) AND NOT knows(S, L).
Your Turn — Your Turn 20.1.4B — Write a multi_ready rule
Write a rule: a member S is multilingual-ready for L if S knows python and S knows L where L is a language.
Hint:
  • Keep it to the conditions you are sure of: knows(S, python) AND knows(S, L) AND language(L).

32.5 Building a Knowledge Base — Full Exam-Style Question

Why are we doing this?
  • 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.

Paper 3 · declarative · [11 marks]
  • 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)

Lab Task — Show full mark scheme
Reveal the full mark scheme for parts (a)–(c).
Hint:
  • (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).

Key Points Summary

A programming paradigm is a style or approach to writing programs. The 9618 syllabus names four: low-level, imperative (procedural), object-oriented, and declarative.
Imperative programming tells the computer HOW — exact steps using variables, assignment, and loops in a set order. Python, Java, and VB.NET are imperative.
Declarative programming tells the computer WHAT is true — facts and rules, then ask questions (goals). The system searches the knowledge base to satisfy the goal. You never write a loop.
To describe declarative: states what, not how (1); uses facts and rules, and goals (queries) to obtain results (1). Two separate points for two marks.
A fact is a single piece of information stated as always true: predicate(arguments). — lower-case, ending in a full stop. E.g. knows(aymaan, python).
The predicate is the relationship name (before the brackets). The arguments are the values inside the brackets — all lower-case in facts.
Argument order carries meaning: mentors(musarrat, naib) is NOT the same as mentors(naib, musarrat). A fact with swapped arguments scores zero.
A variable is written with a Capital letter (e.g. X, Student). Lower-case = value; Capital = variable. Variables belong in goals and rules, never in facts.
A goal (query) is a question asked of the knowledge base. The system tries to satisfy it by finding matching facts — this matching is called instantiation.
A goal with a variable returns EVERY value that satisfies it. Scan all facts, keep the matches, list them all. If no fact matches, the goal fails (returns nothing).
Putting a variable in a different position changes the question: knows(X, python) asks "who knows python?"; knows(naib, L) asks "which languages does Naib know?".
A rule has a head (the new relationship) and a body (the conditions), joined by IF: head(Args) IF condition1 AND condition2 AND NOT condition3.
Translate phrase by phrase: "is/knows/has" = positive condition; "does not" = NOT; "and" = AND; "or" = OR. Re-use the same variable names from the head inside the body.
A 4-mark rule = one mark per condition (each predicate correct, variables in the right place) + one mark for all operators/punctuation correct, no extra code. A comma may replace AND.
Design workflow: 1. Choose predicates, 2. Write facts (known information), 3. Write rules (new relationships not stored directly).
Read the clause meanings table first in a combined question — it tells you the exact argument order for every predicate, which is the difference between full marks and zero.

32.6 Practice Tasks

Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked solution.

1Practice Task — Describe declarative [2 marks]
Describe what is meant by a declarative programming language.
2Practice Task — Describe imperative [2 marks]
Describe what is meant by an imperative (procedural) programming language.
3Practice Task — Identify the paradigm [4 marks]
For each fragment, state the paradigm: (i) male(naib). parent(naib, adri). (ii) total <- 0; WHILE n > 0 (iii) class Student: def __init__(self) (iv) LDD Count; INC ACC; STO Count
4Practice Task — Write facts from a sentence [3 marks]
Mohar is a new club member who knows pseudocode and struggles with Java. Write the additional clauses.
5Practice Task — Write facts with mentors [3 marks]
Nazifa is a new club member who knows Java and mentors Naib. Write the additional clauses.
6Practice Task — Library fact [1 marks]
In a library system where borrowed(naib, novel) means "Naib borrowed the book titled novel", write "Aymaan borrowed the book titled poems".
7Practice Task — What does a goal return? [1 marks]
Given knows(musarrat, python), knows(tarnima, java), knows(aymaan, python), knows(mehrin, vbnet), knows(naib, python), what does ? knows(X, python) return?
8Practice Task — Goal with different position [1 marks]
Using the same knowledge base, what does ? knows(naib, L) return?
9Practice Task — Goal returns nothing [2 marks]
What does ? knows(X, cobol) return, and why?
10Practice Task — Write a rule (can_help) [4 marks]
A member S can help with language L if S knows L and S does not struggle with L. Write this as a rule.
11Practice Task — Write a rule (specialist) [4 marks]
A member S is a specialist in language L if S knows L and S mentors at least one other member. Write this as a rule.
12Practice Task — Write a rule (beginner) [4 marks]
A member S is a beginner in L if L is a language and S does not know L. Write this as a rule.
13Practice Task — Write a rule (should_revise) [4 marks]
A member S should revise language L if L is a language and S struggles with L and S does not have a mentor. Treat "has a mentor" as mentors(M, S). Write this as a rule.
14Practice Task — Full exam: hospital (a)+(b) [7 marks]
Hospital KB: nurses, wards, assigned, trained, unavailable. (a) Nawal is a nurse, assigned to surgery, trained in trauma — write clauses [4]. (b)(i) What does trained(N, trauma) return? [1] (b)(ii) What does ward(W) return, and why does assigned(naib, W) return nothing? [2]
15Practice Task — Full exam: hospital (c) rule [4 marks]
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).

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

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.