Skip to content

9618 · Unit 27 · Paper 3: Advanced theory

Declarative facts, rules and goals.

9618 · Unit 27 · Declarative facts, rules and goals
Revisionbyexamplicity

Facts and predicates

§20.1 · Declarative programming

Some problems are easier to describe than to solve step by step. A declarative solution states relationships rather than an ordered sequence of updates, and the computer finds the answers. Tavren’s knowledge base records each lantern’s connector and which connector a hire accepts.

Tavren’s facts · Prolog notation
connector(31, round).
connector(14, flat).
connector(27, round).
connector(8, round).
connector(22, flat).
connector(19, flat).
accepts(courtyard, round).
accepts(foyer, flat).

What each argument means

A fact means something only if its arguments are always read in the same order. Laid out as two small tables, the facts show what each position holds; be able to say what each argument means.

connector/21: lantern ID2: connector31round14flat27round8round22flat19flataccepts/21: hire2: connectorcourtyardroundfoyerflatone row = one factarity 2 = two arguments
  • connector/2 is a predicate with arity 2: two arguments.
  • A clause is a fact or a rule. A fact asserts that a relation holds; a rule’s conclusion depends on conditions.

Constant or variable?

One capital letter changes what a word means.

  • round and courtyard are lower-case atoms: symbolic constants. 31 is a numeric constant.
  • L and Connector are variables: their values are found by matching. round is fixed; Round would be a variable.
  • A full stop ends a fact or rule.
To add “lantern 19 has a flat connector”, keep the existing order: connector(19, flat). Reversing its arguments asserts a different relationship.
Your turn

1 Write the fact: lantern 40 has a round connector.

2 What is the arity of accepts?

3 Which of these is a variable: round, Round, courtyard?

  1. 1
    connector(40, round).
  2. 2
    2
  3. 3
    Round: it starts with a capital letter
Tick what you can do now
Practise with s26/31 Q11(a)
1 / 5
9618 · Unit 27 · Declarative facts, rules and goals
Revisionbyexamplicity

Goals and bindings

§20.1 · Declarative programming

Facts are stored to be questioned. A goal, or query, asks whether a relationship can be satisfied. Unification matches it with each fact; a matching variable is instantiated (bound) to that value.

The connector facts, in search order, are 31 round, 14 flat, 27 round, 8 round, 22 flat, 19 flat. The courtyard accepts round; the foyer accepts flat.

How a goal is matched

Read down: which facts are tried, which match, and what L becomes.

connector(L, round).facts tried, top to bottomresultconnector(31, round).matches: L = 31connector(14, flat).fails: flat ≠ roundconnector(27, round).matches: L = 27connector(8, round).matches: L = 8connector(22, flat).fails: flat ≠ roundconnector(19, flat).fails: flat ≠ round

Two subgoals that share C must agree. Follow L = 14, then L = 31: the C bound by the first subgoal is the one the second must accept.

connector(L, C), accepts(courtyard, C)Two illustrative trials · same C in both subgoalsL = 14connector matchesC = flatbinding retainedcourtyardaccepts?round required → failsL = 31connector matchesC = roundbinding retainedcourtyardaccepts?round required → succeedsDiscard L = 14 and C = flat before trying L = 31.

Five goals on the same facts. Cover each result and predict it first.

Goal on the stated factsResult
connector(31, round).Succeeds: this exact fact is present.
connector(14, round).Fails: lantern 14 is recorded with flat, not round.
connector(L, round).L = 31; then L = 27; then L = 8.
connector(22, C).C = flat.
connector(L, C).Returns pairs: (31, round), (14, flat), (27, round), (8, round), (22, flat), (19, flat).

In connector(L, C), accepts(courtyard, C), the same C must satisfy both subgoals. A connector match alone does not show that the hire accepts it.

The anonymous variable _

connector(22, _). asks only whether lantern 22 has a connector. The anonymous variable _ matches without returning a value. Each _ is independent; a named variable used twice must take the same value both times.

A goal’s failure means it cannot be proved from this knowledge base. It does not establish that the relationship is impossible in the real world.
Your turn

1 What does connector(L, flat). return?

2 What does connector(27, C). return?

3 Which lanterns does connector(L, C), accepts(foyer, C). find?

  1. 1
    L = 14; then L = 22; then L = 19
  2. 2
    C = round
  3. 3
    14, 22, 19: the flat ones
Tick what you can do now
Practise with s26/31 Q11(b)
2 / 5
9618 · Unit 27 · Declarative facts, rules and goals
Revisionbyexamplicity

Rules

§20.1 · Declarative programming

Facts only record what someone typed in. A rule lets the program work out something new: it infers a relationship when all its conditions hold. Its head is true IF every subgoal in its body succeeds.

Tavren’s connectors: 31 round, 14 flat, 27 round, 8 round, 22 flat, 19 flat. The courtyard accepts round; the foyer accepts flat. Add these facts:

More facts: lanterns, availability and reservations
lantern(31).  lantern(14).  lantern(27).
lantern(8).   lantern(22).  lantern(19).
available(31).
available(8).
available(22).
reserved(8).

The rule uses those facts to decide which lantern can go to which hire:

Rule · head :- body.
can_supply(L, H) :-
lantern(L),
connector(L, C),
accepts(H, C),
available(L),
\+ reserved(L).

Read the rule, subgoal by subgoal

Read the body top to bottom: each subgoal either binds a variable or tests one.

can_supply(L, H):- IF every subgoal succeedslantern(L)chooses Lconnector(L, C)AND binds C for that Laccepts(H, C)AND tests: hire H takes Cavailable(L)AND tests L\+ reserved(L)AND must fail for L
PartIts job
can_supply(L, H)The head is the relationship to establish.
lantern(L)Choose a lantern ID for L before testing its conditions.
connector(L, C), accepts(H, C)The same connector C must link lantern L to hire H.
available(L)The candidate lantern must appear in the availability facts.
\+ reserved(L)The reservation subgoal must fail for this already-bound L.
  • :- reads “if”; the comma joins subgoals with AND.
  • \+ is negation as failure: it succeeds when its subgoal cannot be proved.
  • Every reservation is listed here, so a missing reservation fact can be used for this decision.
Bind L before testing \+ reserved(L). With L unbound, the test asks whether any reservation can be found; it does not enumerate unreserved lanterns. In task notation, the same logic may use IF, AND and NOT.
Your turn

1 Does can_supply(22, foyer). succeed?

2 Does can_supply(14, foyer). succeed? If not, which subgoal fails?

3 When does \+ reserved(L) succeed?

  1. 1
    yes: flat matches, 22 is available and not reserved
  2. 2
    no: available(14) fails
  3. 3
    when reserved(L) cannot be proved for that L
Tick what you can do now
Practise with s26/31 Q11(c)
3 / 5
9618 · Unit 27 · Declarative facts, rules and goals
Revisionbyexamplicity

Tracing inference and backtracking

§20.1 · Declarative programming

The rule says what a suitable lantern is, not how to find one, so the search works through the lanterns one by one. For can_supply(L, courtyard), inference tries the rule’s subgoals in order. Backtracking goes back to an earlier choice after a failure, or when another answer is asked for, and undoes its bindings.

  • Lanterns are tried in the order 31, 14, 27, 8, 22, 19.
  • The courtyard accepts round. Available: 31, 8, 22. Reserved: 8.
Goal · find lantern IDs, including all alternatives
can_supply(L, courtyard).

Start with lantern 8, the most instructive failure: three subgoals succeed, and only the last one sends the search back.

One branch, subgoal by subgoal: L = 8
LineStatement that runsLCWritesWhat happens
1lantern(L)8L 8the next lantern fact offers 8
the next lantern fact offers 8
2connector(8, C)roundC roundonly connector(8, round) matches
only connector(8, round) matches
3accepts(courtyard, round)test a fact matches: succeeds
test a fact matches: succeeds
4available(8)test a fact matches: succeeds
test a fact matches: succeeds
5\+ reserved(8)test reserved(8) is a fact, so the negation fails: undo L and C, try the next lantern
test reserved(8) is a fact, so the negation fails: undo L and C, try the next lantern

The whole search, one row per lantern tried

Read down in search order. The first row where every subgoal succeeds is the first answer returned.

C binds toacceptsavailable\+ reservedWhat happens
L = 31roundyesyesyesevery subgoal succeeds: answer L = 31
every subgoal succeeds: answer L = 31
L = 14flatno——accepts(courtyard, flat) fails: back to lantern(L)
accepts(courtyard, flat) fails: back to lantern(L)
L = 27roundyesno—available(27) fails: back to lantern(L)
available(27) fails: back to lantern(L)
L = 8roundyesyesnoreserved(8) succeeds, so \+ fails: back to lantern(L)
reserved(8) succeeds, so \+ fails: back to lantern(L)
L = 22flatno——accepts(courtyard, flat) fails: back to lantern(L)
accepts(courtyard, flat) fails: back to lantern(L)
L = 19flatno——accepts(courtyard, flat) fails: back to lantern(L)
accepts(courtyard, flat) fails: back to lantern(L)
Try LC binds toOutcome for this candidate
31roundall subgoals succeed: return this binding
14flataccepts(courtyard, flat) fails
27roundavailable(27) fails
8roundreserved(8) succeeds: negation fails
22flataccepts(courtyard, flat) fails
19flataccepts(courtyard, flat) fails
1
lantern(L) offers L = 8. connector(8, C) binds C = round.
2
accepts(courtyard, round) and available(8) both succeed.
3
reserved(8) succeeds, so its negation fails. This candidate cannot satisfy the rule.
4
Undo L = 8 and C = round when leaving that choice. Try the next lantern with fresh bindings.

Returned answer: L = 31. Asking for another answer explores the remaining lanterns, then fails when none is left. That means no more answers; it does not cancel the earlier answer.

The facts’ order affects this search order. It does not change which candidates satisfy this finite rule. A successful connector match alone is insufficient: every subgoal must succeed.
Your turn

1 Trace can_supply(L, foyer). Which answer is returned, and which subgoal stops lantern 14?

2 The fact reserved(8). is deleted. Which answers does can_supply(L, courtyard). give now?

3 What does backtracking undo?

  1. 1
    L = 22; lantern 14 is stopped by available(14)
  2. 2
    L = 31; then L = 8
  3. 3
    the bindings made since the choice it returns to
Tick what you can do now
Practise with s26/31 Q11(b); s26/31 Q11(c)
4 / 5
9618 · Unit 27 · Declarative facts, rules and goals
Revisionbyexamplicity

Solving a declarative task

§20.1 · Declarative programming

Some hires are far from any socket, so Tavren needs a lantern that runs on a battery. A new rule, portable, adds that one condition to can_supply: the lantern must have a battery.

  • can_supply needs a matching connector, availability and no reservation.
  • Round: 31, 27, 8; flat: 14, 22, 19. The courtyard takes round; the foyer takes flat.
  • Available: 31, 8, 22. Reserved: 8.
Extension · these are the complete battery facts
battery(31).
battery(27).

portable(L, H) :-
can_supply(L, H),
battery(L).

can_supply first, then the battery test

Each lane runs can_supply first; only a lantern that passes it reaches the battery test.

portable(L, courtyard)can_supply: L = 31battery(31) succeedsL = 31portable(L, foyer)can_supply: L = 22battery(22) failsgoal fails

Four goals to check against the lanes. The second shows that one condition cannot make up for another.

GoalResult and reason
portable(L, courtyard).L = 31. It satisfies can_supply and has a battery.
portable(27, courtyard).Fails. Having a battery does not override its repair status; available(27) fails.
can_supply(L, foyer).L = 22. Its flat connector and available status satisfy the original rule.
portable(L, foyer).Fails. No battery is recorded for the only eligible lantern, 22.

How to write and check a solution

The same four moves work for any declarative task:

1
Name the relation and the meaning/order of its arguments.
2
Translate each stated relationship into a fact, retaining the existing constants.
3
Write the rule’s head and connect its body using consistent variables.
4
Check a known success, a candidate that fails one condition, and a variable goal that finds all bindings.

Set the declarative answer against the loop you would write procedurally:

ApproachHow the selection is expressed
ProceduralLoop over lanterns, test each condition and append matches to a result array.
DeclarativeState the facts and conditions of portable; ask the goal and let inference seek bindings.
  • Two clauses with the same head act as OR: either body can establish it.
  • Inside one rule, every subgoal is required. A second clause without the battery condition would admit lanterns the requirement excludes.
Your turn

1 The fact battery(22). is added. What does portable(L, foyer). return?

2 Does portable(31, courtyard). succeed?

3 How do you write “A holds if B or C” in this notation?

  1. 1
    L = 22
  2. 2
    yes
  3. 3
    two clauses with the same head: a :- b. and a :- c.
Tick what you can do now
Practise with s26/31 Q11(a); s26/31 Q11(c)
5 / 5