Facts and predicates
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.
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/2is 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.
roundandcourtyardare lower-case atoms: symbolic constants. 31 is a numeric constant.LandConnectorare variables: their values are found by matching.roundis fixed;Roundwould be a variable.- A full stop ends a fact or rule.
connector(19, flat). Reversing its arguments asserts a different relationship.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
connector(40, round). - 22
- 3
Round: it starts with a capital letter
Goals and bindings
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.
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.
Five goals on the same facts. Cover each result and predict it first.
| Goal on the stated facts | Result |
|---|---|
| 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.
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?
- 1L = 14; then L = 22; then L = 19
- 2C = round
- 314, 22, 19: the flat ones
Rules
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:
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:
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.
| Part | Its 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.
\+ 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.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?
- 1yes: flat matches, 22 is available and not reserved
- 2no: available(14) fails
- 3when
reserved(L)cannot be proved for that L
Tracing inference and backtracking
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.
can_supply(L, courtyard).
Start with lantern 8, the most instructive failure: three subgoals succeed, and only the last one sends the search back.
| Line | Statement that runs | L | C | Writes | What happens |
|---|---|---|---|---|---|
| 1 | lantern(L) | 8 | L 8 | the next lantern fact offers 8 | |
| the next lantern fact offers 8 | |||||
| 2 | connector(8, C) | round | C round | only connector(8, round) matches | |
| only connector(8, round) matches | |||||
| 3 | accepts(courtyard, round) | test a fact matches: succeeds | |||
| test a fact matches: succeeds | |||||
| 4 | available(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 to | accepts | available | \+ reserved | What happens | |
|---|---|---|---|---|---|
| L = 31 | round | yes | yes | yes | every subgoal succeeds: answer L = 31 |
| every subgoal succeeds: answer L = 31 | |||||
| L = 14 | flat | no | — | — | accepts(courtyard, flat) fails: back to lantern(L) |
| accepts(courtyard, flat) fails: back to lantern(L) | |||||
| L = 27 | round | yes | no | — | available(27) fails: back to lantern(L) |
| available(27) fails: back to lantern(L) | |||||
| L = 8 | round | yes | yes | no | reserved(8) succeeds, so \+ fails: back to lantern(L) |
reserved(8) succeeds, so \+ fails: back to lantern(L) | |||||
| L = 22 | flat | no | — | — | accepts(courtyard, flat) fails: back to lantern(L) |
| accepts(courtyard, flat) fails: back to lantern(L) | |||||
| L = 19 | flat | no | — | — | accepts(courtyard, flat) fails: back to lantern(L) |
| accepts(courtyard, flat) fails: back to lantern(L) | |||||
| Try L | C binds to | Outcome for this candidate |
|---|---|---|
| 31 | round | all subgoals succeed: return this binding |
| 14 | flat | accepts(courtyard, flat) fails |
| 27 | round | available(27) fails |
| 8 | round | reserved(8) succeeds: negation fails |
| 22 | flat | accepts(courtyard, flat) fails |
| 19 | flat | accepts(courtyard, flat) fails |
lantern(L) offers L = 8. connector(8, C) binds C = round.accepts(courtyard, round) and available(8) both succeed.reserved(8) succeeds, so its negation fails. This candidate cannot satisfy the rule.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.
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?
- 1L = 22; lantern 14 is stopped by available(14)
- 2L = 31; then L = 8
- 3the bindings made since the choice it returns to
Solving a declarative task
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.
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.
Four goals to check against the lanes. The second shows that one condition cannot make up for another.
| Goal | Result 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:
Set the declarative answer against the loop you would write procedurally:
| Approach | How the selection is expressed |
|---|---|
| Procedural | Loop over lanterns, test each condition and append matches to a result array. |
| Declarative | State 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.
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?
- 1L = 22
- 2yes
- 3two clauses with the same head:
a :- b.anda :- c.