Algorithmcore
Justification-based truth maintenance
Also called JTMS, Doyle truth maintenance system.
Records a justification for each derived belief and relabels beliefs in and out when assumptions change, keeping the belief set well founded.
- Time
- O(n s) worst
- Space
- O(n + s) extra
- Where
- n = the number of nodes; s = the total size of all justifications
- Model
- Word RAM
- Properties
- deterministicincremental
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Jon Doyle, 1979
A justification-based truth maintenance system (JTMS) is a bookkeeping layer under a reasoner. The reasoner tells it the justifications of its beliefs, each of the form "believe if all of these nodes are IN and all of those are OUT", and the JTMS labels each node IN or OUT so that every IN node has a non-circular argument from the current premises. When a premise is retracted, or a new justification arrives, it relabels only the nodes that depend on the change. Doyle's system of 1979 allowed out-lists, which make the beliefs nonmonotonic: a node can go OUT because another node came IN. This page gives the logic behind the labels, the well-founded semantics of the network read as a logic program, and an incremental relabelling procedure that computes it, with a third label UNDEC for the nodes that the semantics leaves open. The labels Doyle's own procedure picks on such nodes correspond to the stable models of the network.
In plain words
A reasoning program draws many conclusions, and some of them rest on guesses that may turn out to be wrong. A truth maintenance system keeps, for each conclusion, the reasons that support it. A reason can say "believe this if those things are believed" and also "believe this unless that other thing is believed". When a guess is withdrawn, the system finds every conclusion that rested on it and checks again whether some other reason still supports it. A conclusion that only supports itself in a circle is not believed. When reasons contradict each other in a circle, as in "believe A unless B" and "believe B unless A", the system leaves both open in place of picking one at random.
The problem
A network is a finite set of nodes and justifications , read as the rule
with in-list and out-list . A premise has both lists empty; an assumption is a premise that the reasoner can retract. The network is a ground normal logic program. Its semantics here is the well-founded model , the least fixed point of the operator , where gives the nodes with a justification whose in-list is in and whose out-list is in , and is the greatest unfounded set: the largest set such that each justification of a node of has an in-node in or an out-node in . The labels are IN for , OUT for and UNDEC for the rest.
The task is incremental: keep the labels equal to the well-founded model while justifications are added and removed one at a time. For a network without a cycle through an out-list the model is two-valued and equals the unique stable model, and so the unique labelling in Doyle's sense. With even cycles through out-lists there can be several stable models, and with odd cycles there can be none; deciding whether a network has a stable model is NP-complete (Elkan 1990), while the well-founded model can be computed in polynomial time.
The algorithm
After a justification of is added or removed, RELABEL runs. The affected set is and every node that depends on , directly or through other nodes, through the in-list or the out-list of a justification. The labels outside stay as they are.
RELABEL(x)- 1
S ← x and every node that depends on x through in-lists or out-lists, transitively - 2
remove the labels of the nodes of S - 3
repeat - 4
while some unlabeled node y of S has a justification whose in-list is all IN and whose out-list is all OUT do - 5
label y IN - 6
P ← the least set of nodes of S that are not OUT such that y ∈ P when y has a justification whose in-nodes are each IN, UNDEC or in P, and whose out-nodes are not IN - 7
label OUT each unlabeled node of S that is not in P - 8
until line L7 labels no node - 9
label UNDEC each node of S that is still unlabeled
Lines L4 and L5 are the step : a node with a valid justification is IN. Lines L6 and L7 are the step : holds the nodes that could still get a non-circular argument, and every unlabeled node outside is in an unfounded set, so it is OUT. A positive loop, such as , never enters and goes OUT. This is the alternating fixpoint of the well-founded semantics, run only on the affected nodes. A node with no justification is OUT from the start.
Example
The network has the justifications and .
| change | affected set | rounds | labels after |
|---|---|---|---|
| assume | bird, flies | bird IN by the premise; flies IN: bird is IN and abnormal is OUT | bird IN, flies IN, abnormal OUT, penguin OUT |
| assume | penguin, abnormal, flies | penguin IN, abnormal IN; flies has the out-node abnormal IN, so it is not in and goes OUT | penguin IN, abnormal IN, flies OUT |
| retract | penguin, abnormal, flies | round 1: no node gets IN; , so penguin and abnormal go OUT; round 2: flies IN | penguin OUT, abnormal OUT, flies IN |
Now add and . Neither justification is valid while both nodes are unlabeled, and both nodes are in , so both end UNDEC. The network has two stable models here, and Doyle's procedure would pick one of them. Assuming makes IN and then OUT.
Correctness
The correctness proof has three parts. First, the nodes outside do not depend on the change, so by the splitting set theorem their old labels are still their well-founded labels. Second, each IN label is given by a justification whose nodes are already correctly IN and OUT, and each OUT label goes to a set that is unfounded for the current labels, so every label is a well-founded label (soundness). Third, when the loop stops, the IN and OUT sets form a fixed point of , and since the well-founded model is the least fixed point and the labels lie below it, the two are equal.
Proof: Incremental relabelling of a JTMS gives the well-founded labellingfull
A network is a finite set of justifications , read as the rules of a ground normal logic program. A partial labelling is a pair of disjoint sets of nodes, the IN nodes and the OUT nodes; the other nodes are undecided. We order pairs by when and .
Definitions and known facts
- is the set of nodes with a justification whose in-list is inside and whose out-list is inside .
- A set is unfounded with respect to when each justification with has an in-node in or an out-node in . A union of unfounded sets is unfounded, so there is a greatest unfounded set .
- . Both parts grow when grows, so is monotone, and the well-founded model is its least fixed point (Van Gelder, Ross and Schlipf 1991; see well-founded semantics). We write it for the network after the change.
Lemma 1 (supported nodes). For a partial labelling , let be the least set of nodes such that whenever has a justification with no in-node in , every in-node in , and no out-node in . Then is the complement of .
Proof. A node fails the rule for each of its justifications, so each has an in-node in , an in-node outside , or an out-node in . So the complement of is unfounded. Conversely, let be unfounded and build in stages. A node enters by a justification whose in-nodes are in already, hence not in by induction, and not in , and whose out-nodes are not in . This justification breaks the unfounded condition, so the node is not in .
Lemma 2 (splitting). Let be a set of nodes such that every justification of a node of mentions only nodes of , and let be the justifications of the nodes of . Then the well-founded model of , restricted to , is the well-founded model of .
Proof. For , membership of in depends only on and . A set is unfounded for exactly when it is unfounded for , and is unfounded, since the justifications of its nodes mention only nodes of . So restricted to is of the restriction, and by induction the iterates of and from agree on . This is the splitting set theorem for the well-founded semantics.
The procedure
We use RELABEL of the page, called after a justification of was added or removed, when the labels were the well-founded model of the network before the change. Line L1 computes the set of nodes that depend on . Let be the set of the other nodes.
Lemma 3. No justification of a node of mentions a node of , and the justifications of the nodes of did not change. So after line L2, every node of carries its label in , with UNDEC for undecided.
Proof. If a justification of mentions , then depends on and line L1 puts in . The changed justification belongs to . By Lemma 2, applied before and after the change, the labels of are the well-founded labels of , which is the same network before and after.
Lemma 4 (soundness). At every moment, the IN nodes are in and the OUT nodes are in .
Proof. By Lemma 3 this holds after line L2, and the labels of never change afterwards. We show that each new label keeps it.
Line L5 labels IN by a justification whose in-nodes are IN, so in , and whose out-nodes are OUT, so in . Then .
Line L7 labels OUT the set of unlabeled nodes of that are not in . Take and a justification . Since is not OUT and , this justification breaks the rule of line L6: either an out-node is IN, or an in-node is not IN, not UNDEC and not in . In the second case is OUT, or is unlabeled, and then (all nodes of are labelled) and . So is unfounded with respect to the current labels, and since unfoundedness is kept when the labels grow, is unfounded with respect to . So .
Lemma 5 (termination). Each round of lines L3 to L8, except the last, labels a node at line L7. Each pass of line L4 labels a node or ends the inner loop. Labels in only change from none to a label. So the procedure stops.
Theorem
When RELABEL stops, IN, OUT and UNDEC are exactly the true, false and undefined nodes of the well-founded model of the new network.
Proof. Let be the final IN and OUT sets. By Lemma 4, . We show that is a fixed point of . Since is the least fixed point, then , and the two are equal.
. On this holds by Lemma 3 and Lemma 2, because is a fixed point and the labels on agree with it. A node of labelled IN got its label by a justification that was valid at that time, and labels never change once set, so the justification is still valid. A node of labelled UNDEC was unlabeled in the last pass of line L4, so it has no valid justification. A node of labelled OUT is in ; if a justification of were valid in , it would be valid in and put in , which is disjoint from .
. On this holds as before. Each node of labelled OUT at line L7 belongs to a set that was unfounded for the labels of that time, and so for ; so . For the other direction we use Lemma 1. In the last round, line L7 labelled nothing, so every node of that ends UNDEC is in the set of that round, and the IN labels did not change after it. The nodes of , the UNDEC nodes of and the nodes of are all in : the nodes of because on is the complement of by Lemma 2; an IN node of by induction on the order of labelling, through the justification that made it IN; a node of by induction on the stages of , through the justification that put it in , whose in-nodes are IN, UNDEC or in , so not OUT, and whose out-nodes are not IN. So , the complement of , holds only OUT nodes.
Corollaries
- If the network has no cycle through an out-list (it is stratified), its well-founded model is two-valued, so no node ends UNDEC, and the labelling is the unique stable model.
- If the final labelling has no UNDEC node, the network has exactly one stable model, and it is the set of IN nodes. This is the general fact that a two-valued well-founded model is the unique stable model.
- The labels do not depend on the order of the changes, only on the final network, because each call ends at the well-founded model.
Complexity
The repeat loop runs at most rounds, since each round but the last labels a node OUT. With a counter for each justification, as in Horn-SAT forward chaining, lines L4 to L7 cost in each round. So one change costs in the worst case, and when only the affected part , with justifications of total size , is scanned. The implementation rescans the affected justifications without counters, for clarity. The space is for the nodes, the justifications and the lists of dependents. The computation is polynomial because it does not choose between stable models; finding a two-valued labelling of the kind that Doyle's procedure looks for is NP-complete (Elkan 1990).
Properties
- Incremental. Only the nodes that depend on the change are relabelled.
- Well founded. An IN node has a non-circular argument from the premises; a node that only supports itself is OUT.
- Cautious. Where the network leaves a choice, as in an even loop through out-lists, the labels say UNDEC. Doyle's labelling instead picks a stable model.
- Order independent. The labels depend only on the current network, not on the order of past changes.
- Single context. A JTMS holds one set of beliefs at a time. To compare different sets of assumptions, it must relabel each time; the ATMS keeps all contexts at once.
Variants and improvements
- Contradictions and dependency-directed backtracking. A contradiction node marks a set of beliefs that cannot hold together. When it goes IN, Doyle's system traces its justifications back to the assumptions under it, records a nogood, and adds a justification that makes one of those assumptions OUT. This is smarter than chronological backtracking, which undoes the last choice whatever it was. This page describes it but does not implement it.
- Monotonic JTMS. With empty out-lists, labelling is forward chaining on Horn clauses, and retraction is the removal of the nodes that lost all support, as in incremental view maintenance.
- Stable labellings. Doyle's procedure, and later TMS designs, choose a two-valued labelling on even loops; these labellings are the stable models of the network (Elkan 1990). See stable model semantics.
- LTMS. McAllester's logic-based TMS takes clauses in place of justifications and labels nodes by unit propagation.
- ATMS. The assumption-based TMS of de Kleer labels each node with the minimal sets of assumptions under which it holds.
When to use it
Use a JTMS under a rule engine or a planner that makes assumptions and must withdraw them cheaply, when one set of beliefs is active at a time. It is the cache of a production system whose conclusions must disappear when their premises do. Use an ATMS when many alternative sets of assumptions must be compared at the same time, as in diagnosis.
Pitfalls
- Circular support. A naive relabeller that keeps a node IN while some justification is valid can keep a self-supporting loop IN after its real support is gone. The unfounded step of lines L6 and L7 is what rules this out.
- Odd loops. A justification such as has no two-valued labelling, so a procedure that looks for one, as Doyle's does, cannot succeed on such a network. The well-founded labels give UNDEC.
- Relabelling too much. Relabelling the whole network after each change is correct but slow; the affected set must include the nodes that depend through out-lists too, or the labels go wrong.
- Mixing up OUT and false. OUT means "not believed now", not "believed false". A node and its negation are two separate nodes, tied together only by a contradiction node.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# A justification-based truth maintenance system for the page
# https://algorithm.tamnd.com/justification-based-truth-maintenance/. The authors are listed in prov/.
"""A justification-based truth maintenance system (JTMS) with nonmonotonic justifications.
A node is any hashable value. A justification of a node x has an in-list and
an out-list: it is valid when every node of the in-list is IN and every node
of the out-list is OUT, and then x is believed. A justification with both lists
empty is a premise; an assumption is a premise that can be removed again.
Each node has the label IN, OUT or UNDEC. The labels are the well-founded
model of the network read as the normal logic program x <- ins, not outs:
IN is true, OUT is false and UNDEC is undefined. After each change only the
changed node and the nodes that depend on it are labelled again.
"""
IN, OUT, UNDEC = "IN", "OUT", "UNDEC"
class JTMS:
def __init__(self):
self.justs = {} # id -> (node, ins, outs)
self.of_node = {} # node -> set of justification ids
self.users = {} # node -> set of nodes with a justification that mentions it
self.label = {}
self.next_id = 0
self.relabelled = 0 # the number of nodes that relabel has visited
def add_node(self, x):
if x not in self.label:
self.label[x] = OUT
self.of_node[x] = set()
self.users[x] = set()
def justify(self, x, ins=(), outs=()):
"""Add the justification x <- ins, not outs and return its id."""
for y in (x, *ins, *outs):
self.add_node(y)
jid = self.next_id
self.next_id += 1
self.justs[jid] = (x, tuple(ins), tuple(outs))
self.of_node[x].add(jid)
for y in (*ins, *outs):
self.users[y].add(x)
self.relabel(x)
return jid
def assume(self, x):
return self.justify(x)
def retract(self, jid):
"""Remove a justification. The users lists may keep x; a larger closed set to relabel is still correct."""
x, _, _ = self.justs.pop(jid)
self.of_node[x].discard(jid)
self.relabel(x)
def is_in(self, x):
return self.label.get(x) == IN
def _valid(self, jid):
_, ins, outs = self.justs[jid]
return all(self.label[y] == IN for y in ins) and all(self.label[y] == OUT for y in outs)
def relabel(self, x):
affected, stack = {x}, [x] # @line L1
while stack:
y = stack.pop()
for z in self.users[y]:
if z not in affected:
affected.add(z)
stack.append(z)
self.relabelled += len(affected)
for y in affected: # @line L2
self.label[y] = None
while True: # @line L3
changed = True
while changed: # @line L4
changed = False
for y in affected:
if self.label[y] is None and any(self._valid(j) for j in self.of_node[y]):
self.label[y] = IN # @line L5
changed = True
possible = set() # @line L6
grew = True
while grew:
grew = False
for y in affected:
if y in possible or self.label[y] == OUT:
continue
for j in self.of_node[y]:
_, ins, outs = self.justs[j]
if all(self.label[b] in (IN, UNDEC) or b in possible for b in ins) and all(self.label[c] != IN for c in outs):
possible.add(y)
grew = True
break
new_out = [y for y in affected if self.label[y] is None and y not in possible]
for y in new_out: # @line L7
self.label[y] = OUT
if not new_out: # @line L8
break
for y in affected: # @line L9
if self.label[y] is None:
self.label[y] = UNDEC
test_justification_based_truth_maintenance.py 118 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/justification-based-truth-maintenance/. The authors are listed in prov/.
import random
import unittest
from itertools import combinations
from justification_based_truth_maintenance import IN, OUT, UNDEC, JTMS
def subsets(universe):
for k in range(len(universe) + 1):
yield from combinations(universe, k)
def unfounded(rules, u, true, false):
"""Each rule for an atom of u has an in-node that is false or in u, or an out-node that is true."""
for h, ins, outs in rules:
if h in u and not (any(b in false or b in u for b in ins) or any(c in true for c in outs)):
return False
return True
def wfs_brute(rules, universe):
"""The well-founded model as the least fixpoint of W(T, F) = (T_P(T, F), greatest unfounded set), all by brute force."""
true, false = frozenset(), frozenset()
while True:
new_true = frozenset(h for h, ins, outs in rules if all(b in true for b in ins) and all(c in false for c in outs))
gus = set()
for s in subsets(universe):
if unfounded(rules, set(s), true, false):
gus.update(s)
new_false = frozenset(gus)
if (new_true, new_false) == (true, false):
return true, false
true, false = new_true, new_false
def least_model(definite):
m = set()
while True:
nxt = {h for h, ins in definite if all(b in m for b in ins)}
if nxt <= m:
return m
m |= nxt
def stable_models_brute(rules, universe):
out = []
for s in subsets(universe):
s = set(s)
reduct = [(h, ins) for h, ins, outs in rules if not any(c in s for c in outs)]
if least_model(reduct) == s:
out.append(frozenset(s))
return out
def labels_from(universe, true, false):
return {x: IN if x in true else OUT if x in false else UNDEC for x in universe}
class TestJTMS(unittest.TestCase):
def test_page_example(self):
t = JTMS()
t.justify("flies", ins=["bird"], outs=["abnormal"])
t.justify("abnormal", ins=["penguin"])
a_bird = t.assume("bird")
self.assertEqual((t.label["bird"], t.label["flies"], t.label["abnormal"]), (IN, IN, OUT))
a_penguin = t.assume("penguin")
self.assertEqual((t.label["penguin"], t.label["abnormal"], t.label["flies"]), (IN, IN, OUT))
t.retract(a_penguin)
self.assertEqual((t.label["abnormal"], t.label["flies"]), (OUT, IN))
t.retract(a_bird)
self.assertEqual(t.label["flies"], OUT)
def test_loops(self):
t = JTMS()
t.justify("p", outs=["q"])
t.justify("q", outs=["p"]) # an even loop: two stable models, so the well-founded labels are UNDEC
self.assertEqual((t.label["p"], t.label["q"]), (UNDEC, UNDEC))
t.justify("r", outs=["r"]) # an odd loop: no stable model
self.assertEqual(t.label["r"], UNDEC)
t.justify("s", ins=["s"]) # a positive loop has no support
self.assertEqual(t.label["s"], OUT)
t.assume("q")
self.assertEqual((t.label["p"], t.label["q"]), (OUT, IN))
def test_random_against_brute_force(self):
rng = random.Random(20261115)
undecided = 0
for _ in range(250):
n = rng.randint(1, 8)
universe = list(range(n))
t = JTMS()
for x in universe:
t.add_node(x)
live = {}
for _ in range(14):
if live and rng.random() < 0.35:
jid = rng.choice(sorted(live))
t.retract(jid)
del live[jid]
else:
x = rng.randrange(n)
ins = tuple(rng.sample(universe, rng.randint(0, min(2, n))))
outs = tuple(rng.sample(universe, rng.randint(0, min(2, n))))
live[t.justify(x, ins, outs)] = (x, ins, outs)
rules = list(live.values())
true, false = wfs_brute(rules, universe)
self.assertEqual(t.label, labels_from(universe, true, false))
undecided += len(true) + len(false) < n
if len(true) + len(false) == n:
self.assertEqual(stable_models_brute(rules, universe), [true])
self.assertGreater(undecided, 100) # the random networks do reach the UNDEC case
if __name__ == "__main__":
unittest.main()
History
Doyle (1979) introduced the truth maintenance system at MIT, with SL justifications that have in-lists and out-lists, the requirement of well-founded support, and dependency-directed backtracking from the work of Stallman and Sussman. McAllester gave the logic-based TMS around 1980, and de Kleer (1986) the assumption-based TMS. Elkan (1990) showed that the labellings of a nonmonotonic TMS are the stable models of the network read as a logic program, and that finding one is NP-complete. Forbus and de Kleer (1993) gave the standard textbook treatment.
References
- Jon Doyle, A truth maintenance system, Artificial Intelligence 12(3), 231 to 272 (1979), the definitions of nodes, SL justifications with in-lists and out-lists, well-founded support, the relabelling procedure and dependency-directed backtracking
- Charles Elkan, A rational reconstruction of nonmonotonic truth maintenance systems, Artificial Intelligence 43(2), 219 to 234 (1990), the reading of the labellings of a nonmonotonic TMS as stable models, and the proof that finding one is NP-complete
- Kenneth D. Forbus and Johan de Kleer, Building Problem Solvers, MIT Press (1993), the chapters on the justification-based TMS, its interface to a rule engine, and contradiction handling
- Allen Van Gelder, Kenneth A. Ross and John S. Schlipf, The well-founded semantics for general logic programs, Journal of the ACM 38(3), 620 to 650 (1991), unfounded sets and the definition of the well-founded model as the least fixed point of the operator W
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 1C7A95 Edit this page History Report an error Markdown