Declarative Programming
Writing facts and rules from a scenario, and goals that the language satisfies by matching and backtracking.
Declarative programming: facts, rules and goals
In a declarative program you say what you want, not how to find it; the language searches for answers.
A declarative program is a list of:
- facts: things that are true
- rules: they say how new facts follow from others
You set a goal, which is a question. The language searches the facts and rules for every answer.
| Example | How to write it | |
|---|---|---|
| Fact | parent(bat, dorj). | lower-case constants (fixed values), and a full stop at the end |
| Variable | X, Y and Z in a rule | starts with a capital letter; can stand for anything |
| Goal | grandparent(bat, W) | returns every W that makes it true |
Remember:You say what you want, not how to find it.
parent(bat, dorj).
parent(bat, saraa).
parent(dorj, tuya).
parent(saraa, nomin).
parent(saraa, temuujin).
female(saraa).
female(tuya).
female(nomin).
grandparent(X, Z) IF parent(X, Y) AND parent(Y, Z).
mother(M, C) IF female(M) AND parent(M, C).
goal: parent(bat, X)
X = dorj, saraa
goal: grandparent(bat, W)
W = tuya, nomin, temuujin
goal: mother(M, nomin)
M = saraa
goal: female(bat)
falseFacts: writing down what is true
A fact is one clause: a name, the values it links in brackets, and a full stop.
A fact is one clause (one statement): a name, the values it links in brackets, then a full stop. For example: wants(anu, riding).
Rules for writing a fact:
- Every word is lower case. A word that starts with a capital letter is a variable, not a value.
- Join a two-word value with an underscore, as in
rock_climbing. - Keep the order the given clauses use. wants puts the camper first, so a new wants fact must too.
- A new person or activity also needs a fact of its own.
Example:Khulan is a camper who wants to try rafting and has already done riding. Rafting is new, so that takes four facts.
camper(khulan).
activity(rafting).
wants(khulan, rafting).
done(khulan, riding).Goals: one question, every answer
A goal is a question with a variable where the answer goes; it returns every value that makes it true.
A goal is a question. It is written like a fact, but with a variable where the answer should go.
wants(C, riding) asks: which campers want to do riding? The language checks the facts from the top. It returns every value that makes the goal true: C = anu, saraa.
A goal with no variable, such as done(anu, riding), is simply true or false.
Exam:The answer line already starts C =. Write every value after it, separated by commas, in lower case and with no full stop. A missing value loses the mark, but the values may come in any order.
goal: wants(C, riding)
C = anu, saraa
goal: done(saraa, A)
A = archery, rock_climbing
goal: done(anu, riding)
falseTwo conditions joined by AND
AND joins two conditions; both must be true at once, and a shared variable takes one value in both.
A goal can join two conditions with AND. Both must be true at once. A variable that appears in both must take the same value in both.
Example:wants(C, riding) AND done(C, archery) asks for a camper who wants riding and has already done archery. Anu wants riding but has not done archery. So the answer is C = saraa.
A new letter is a new variable. wants(C, A) AND done(C, riding) finds a camper and an activity together.
Exam:The mark schemes also accept a comma in place of AND. Examiners report that many answers lost marks because the parts were not joined correctly.
goal: wants(C, riding)
AND done(C, archery)
C = saraa
goal: wants(C, A)
AND done(C, riding)
C = bold, A = archeryRules: IF and AND
A rule says when something is true: the head, then IF, then conditions joined by AND, then a full stop.
A rule has four parts, in this order:
- the head: what the rule makes true
- IF
- the conditions, joined by AND
- a full stop
Example rule: teaches(T, C) IF leads(T, A) AND wants(C, A).
It means: T teaches C if T leads an activity A, and C wants to do A.
- The shared A links the two conditions.
- T and C carry the answer out through the head.
A goal uses a rule like a fact: teaches(gerel, C) gives C = anu, saraa.
Exam:The head is given, and you write the rest. Use only the names the question has defined, with their values in the same order.
leads(gerel, riding).
leads(tuya, archery).
teaches(T, C)
IF leads(T, A) AND wants(C, A).
goal: teaches(gerel, C)
C = anu, saraaHow a goal is satisfied: matching and backtracking
The language tries the clauses from the top, gives variables values, and backtracks to find every answer.
The language satisfies a goal by searching:
- It takes the first condition and tries the clauses from the top.
- When a clause matches, each variable takes the matching value: it is instantiated. This is instantiation.
- The search then tries the next condition with those values.
- After an answer, or when a condition fails, it backtracks. It goes back to its last choice and tries the next clause. This is backtracking.
Watch out:The words instantiation and backtracking are not in the syllabus. But they name what the search does.
Example:For teaches(T, saraa), leads(gerel, riding) gives T = gerel and A = riding. wants(saraa, riding) is a fact, so gerel is an answer. Backtracking then tries leads(tuya, archery), but wants(saraa, archery) is not a fact.
So every answer is found, in the clauses' order.
NOT: a condition that must not be true
NOT makes a condition that must not be true; put it last, after its variables have values.
Exam rules usually end with NOT. Take this sentence: C may try A if C is a camper, A is an activity and C has not already done A.
To turn it into a rule:
- Each part becomes a condition.
- "has not" becomes NOT.
- AND joins them.
NOT done(C, A) is true when the search can find no fact done(C, A) for the values C and A have.
Example:For saraa, only riding is left, because she has done archery and rock_climbing.
Watch out:The mark schemes accept the conditions in any order. But Prolog, like this page's search, tries them from left to right. So put NOT last, where C and A already have values.
may_try(C, A)
IF camper(C) AND activity(A)
AND NOT done(C, A).
goal: may_try(saraa, A)
A = ridingRecursive rules: a rule that uses itself
A rule can use itself: a base case and a general case share one head and work like OR.
A rule can use itself, like a recursive function. No exam has asked for a recursive rule yet, but recursion itself is in the syllabus.
An ancestor is a parent, or a parent of an ancestor. So two rules share one head:
- The first rule is the base case: a parent is an ancestor.
- The second rule is the general case. It moves one generation down with
parent(X, Z). Then it asks ancestor again from there. It stops when it reaches someone who is nobody's parent.
Two rules with one head work like OR: either one can prove the goal. So ancestor(bat, W) finds W = dorj, saraa, tuya, anu, however many generations down they are.
Remember:The rule calls itself last, after parent(X, Z) has given Z a value.
ancestor(X, Y) IF parent(X, Y).
ancestor(X, Y)
IF parent(X, Z) AND ancestor(Z, Y).
goal: ancestor(bat, W)
W = dorj, saraa, tuya, anuKEY TERMS
CHECK YOURSELF
1.The facts are parent(bat, dorj). and parent(bat, saraa). What does the goal parent(bat, X) return?
2.Which clause records the fact that Khulan is a camper?
3.The facts are wants(anu, riding). wants(bold, archery). wants(saraa, riding). How should the result of the goal wants(C, riding) be written?
4.The facts are wants(anu, riding). wants(saraa, riding). done(saraa, archery). What does wants(C, riding) AND done(C, archery) return?
5.Which rule says: C may try A if C is a camper, A is an activity and C has not already done A?
6.A search has just found one answer to a goal. What does it do next?
7.ancestor(X, Y) IF parent(X, Y). and ancestor(X, Y) IF parent(X, Z) AND ancestor(Z, Y). Which one is the base case?