Algorithm Search⌘K

Definitioncore

Default extension

Also called default logic extension.

An extension of a default theory is a set of beliefs that is a fixed point of Reiter's operator, so it contains the facts and every default conclusion it does not block.

An extension is what a default theory lets you believe. A default α:β/γ\alpha : \beta / \gamma says "if α\alpha is believed and β\beta can be consistently assumed, believe γ\gamma". The trouble is that "can be consistently assumed" refers to the final set of beliefs, which the defaults are still building. Reiter (1980) solved this with a fixed point: guess the final set EE, use EE only for the consistency tests, derive everything that the facts and the applicable defaults give, and accept EE when the result is EE again. A theory can have one extension, several, or none.

Definition

A default theory is a pair (W,D)(W, D). Here WW is a set of formulas (the facts) and DD is a set of defaults α:β1,…,βn/γ\alpha : \beta_1, \ldots, \beta_n / \gamma with a prerequisite α\alpha, justifications β1,…,βn\beta_1, \ldots, \beta_n and a conclusion γ\gamma. This page uses the propositional case: finitely many atoms, WW and DD finite. Th(S)\mathrm{Th}(S) is the set of the classical consequences of SS, and cons(G)\mathrm{cons}(G) is the set of the conclusions of the defaults in GG.

For a set of formulas SS, let Γ(S)\Gamma(S) be the smallest set TT of formulas with these three properties.

  1. W⊆TW \subseteq T.
  2. Th(T)=T\mathrm{Th}(T) = T.
  3. For each default α:β1,…,βn/γ\alpha : \beta_1, \ldots, \beta_n / \gamma in DD: if α∈T\alpha \in T and ¬βi∉S\neg\beta_i \notin S for each ii, then γ∈T\gamma \in T.

A set EE is an extension of (W,D)(W, D) if Γ(E)=E\Gamma(E) = E (Reiter 1980, Section 2). The argument SS is used only in the consistency test ¬βi∉S\neg\beta_i \notin S. The rest of the construction is a least set, so every formula of an extension has a derivation from WW by classical steps and default steps whose justifications are consistent with the extension.

Iterative form. Reiter's Theorem 2.1 gives an equivalent test. Put E0=WE_0 = W and

Ei+1=Th(Ei)∪{γ:α:β1,…,βn/γ∈D, α∈Ei, ¬βj∉E for each j}.E_{i+1} = \mathrm{Th}(E_i) \cup \{\gamma : \alpha : \beta_1, \ldots, \beta_n / \gamma \in D,\ \alpha \in E_i,\ \neg\beta_j \notin E \text{ for each } j\}.

Then EE is an extension exactly when E=⋃iEiE = \bigcup_i E_i. The candidate EE appears on the right side, so this is a test of a guess, not a way to build EE from nothing.

Generating defaults. If EE is an extension, then E=Th(W∪cons(G))E = \mathrm{Th}(W \cup \mathrm{cons}(G)), where GG is the set of the defaults whose prerequisite is in EE and whose justifications are all consistent with EE. So a finite theory has at most 2∣D∣2^{|D|} extensions, and each is found by trying the subsets of DD:

  1. EXTENSIONS(W, D)
  2. 1found ← ∅
  3. 2for each subset S of D do
  4. 3E ← Th(W ∪ cons(S)) ▷ the candidate
  5. 4G ← ∅
  6. 5repeat ▷ the least G that E allows
  7. 6G' ← {d ∈ D : pre(d) ∈ Th(W ∪ cons(G)) and ¬β ∉ E for each justification β of d}
  8. 7if G' = G then leave the loop
  9. 8G ← G'
  10. 9if Th(W ∪ cons(G)) = E then add E to found ▷ Γ(E) = E
  11. 10return found

Lines L4 to L8 compute Γ(E)\Gamma(E) as Th(W∪cons(G))\mathrm{Th}(W \cup \mathrm{cons}(G)). The sets GG grow, so the loop stops after at most ∣D∣|D| rounds. The implementation keeps each theory as its set of models over the atoms, so a membership test φ∈Th(X)\varphi \in \mathrm{Th}(X) is a check that every model of XX satisfies φ\varphi. This takes time exponential in both ∣D∣|D| and the number of atoms. That is expected: deciding whether an extension exists is Σ2p\Sigma_2^p-complete for finite propositional theories (Gottlob 1992), as the page on the complexity of default logic shows.

Normal defaults. A default is normal if it has the form α:γ/γ\alpha : \gamma / \gamma. Reiter (1980, Section 3) proved three facts about theories whose defaults are all normal. Every such theory has an extension. Adding a normal default keeps each old extension inside some new one (semi-monotonicity). Two different extensions contradict each other (orthogonality).

Examples and non-examples

  • Tweety. W={bird}W = \{\mathit{bird}\} and D={bird:flies/flies}D = \{\mathit{bird} : \mathit{flies} / \mathit{flies}\}. The only extension is Th({bird,flies})\mathrm{Th}(\{\mathit{bird}, \mathit{flies}\}). If WW also contains ¬flies\neg\mathit{flies}, the justification fails, and the only extension is Th(W)\mathrm{Th}(W).
  • Nixon diamond, two extensions. W={q,r}W = \{q, r\} (a Quaker and a Republican) and D={q:p/p, r:¬p/¬p}D = \{q : p / p,\ r : \neg p / \neg p\}. Each default blocks the other. Th({q,r,p})\mathrm{Th}(\{q, r, p\}) and Th({q,r,¬p})\mathrm{Th}(\{q, r, \neg p\}) are extensions. A credulous reasoner may believe either pp or ¬p\neg p. A skeptical reasoner believes only what is in both, and so neither.
  • Non-example: the facts alone. For the Nixon diamond, E=Th({q,r})E = \mathrm{Th}(\{q, r\}) is not an extension. Both pp and ¬p\neg p are consistent with EE, so Γ(E)\Gamma(E) contains both and is inconsistent, which is not EE.
  • No extension. W=∅W = \emptyset and D={⊤:¬a/a}D = \{\top : \neg a / a\}. If a∈Ea \in E, the justification ¬a\neg a fails and Γ(E)=Th(∅)\Gamma(E) = \mathrm{Th}(\emptyset), which does not contain aa. If a∉Ea \notin E, the default applies and a∈Γ(E)a \in \Gamma(E). In both cases Γ(E)≠E\Gamma(E) \neq E. The default is not normal, so this does not contradict Reiter's existence theorem.
  • Non-example: an ungrounded belief. W=∅W = \emptyset and D={a:a/a}D = \{a : a / a\}. The set Th({a})\mathrm{Th}(\{a\}) is not an extension, because the prerequisite aa is not derivable from WW without already having aa, so Γ(Th({a}))=Th(∅)\Gamma(\mathrm{Th}(\{a\})) = \mathrm{Th}(\emptyset). The only extension is Th(∅)\mathrm{Th}(\emptyset). In autoepistemic logic the similar premise La⊃aLa \supset a has the expansion that contains aa.
  • Inconsistent facts. If WW is inconsistent, Γ(E)\Gamma(E) always contains all formulas, so the set of all formulas is the unique extension.
  • Cumulativity fails. Makinson's example, reported by Kraus, Lehmann and Magidor (1990, Section 3.1), uses two normal defaults ⊤:a/a\top : a / a and a∨b:¬a/¬aa \vee b : \neg a / \neg a. With W=∅W = \emptyset the only extension is Th({a})\mathrm{Th}(\{a\}), so aa and a∨ba \vee b are both skeptical consequences. Add the consequence a∨ba \vee b to WW. Now Th({¬a,b})\mathrm{Th}(\{\neg a, b\}) is a second extension, because the second default applies before the first, so aa is no longer a skeptical consequence. Adding a conclusion that you already believed changed your beliefs, so skeptical default logic fails Cautious Monotonicity, one of the rules of System P.
  • Default logic is the logic in which extensions are the semantics, with credulous and skeptical consequence defined from them.
  • The complexity of default logic gives the exact complexity of existence, credulous and skeptical reasoning for finite propositional theories.
  • A stable expansion in autoepistemic logic is the corresponding fixed point for a modal belief operator.
  • A stable model of a normal logic program is an extension in disguise: the rule p←q,not rp \leftarrow q, \mathit{not}\ r reads as the default q:¬r/pq : \neg r / p, and the Gelfond-Lifschitz reduct plays the role of Γ\Gamma.
  • Γ\Gamma is antimonotone: a larger SS blocks more defaults and gives a smaller Γ(S)\Gamma(S). So the Knaster-Tarski theorem does not apply, which is why a fixed point may fail to exist. Γ∘Γ\Gamma \circ \Gamma is monotone, and its least and greatest fixed points give the well-founded semantics of default logic, in the same way as the alternating fixpoint does for programs.
  • Nonmonotonic reasoning places extensions among the other ways of drawing defeasible conclusions.

Code

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.

default_extension.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/default-extension/. The authors are listed in prov/DNSBMF.jsonl.
"""Brute-force search for the extensions of a finite propositional default theory.

A formula is a nested tuple: ("var", name), ("not", f), ("and", f, ...),
("or", f, ...), ("true",) or ("false",). A default alpha : beta_1, ..., beta_n / gamma
is a triple (alpha, [beta_1, ..., beta_n], gamma). A theory is (W, D).

A deductively closed theory over the given atoms is kept as its set of models:
a frozenset of the indices of the worlds that satisfy it. Th(S) contains phi
exactly when every model of S satisfies phi.

extensions(atom_names, W, D) tries each subset S of D, takes E = Th(W and the
conclusions of S) as a candidate, and keeps it when E is a fixed point of
Reiter's operator Gamma. Every extension is generated by a subset of D, so
none is missed. The cost is O(2^|D| |D|^2 2^n) formula evaluations for n atoms.
"""

from itertools import combinations, product
from typing import Dict, FrozenSet, List, Sequence, Tuple

Formula = tuple
Default = Tuple[Formula, Sequence[Formula], Formula]
Models = FrozenSet[int]


def evaluate(f: Formula, world: Dict[str, bool]) -> bool:
    op = f[0]
    if op == "var":
        return world[f[1]]
    if op == "not":
        return not evaluate(f[1], world)
    if op == "and":
        return all(evaluate(g, world) for g in f[1:])
    if op == "or":
        return any(evaluate(g, world) for g in f[1:])
    return op == "true"


def worlds(atom_names: Sequence[str]) -> List[Dict[str, bool]]:
    return [dict(zip(atom_names, v)) for v in product([False, True], repeat=len(atom_names))]


def models(formulas: Sequence[Formula], ws: List[Dict[str, bool]]) -> Models:
    """The models of a set of formulas, that is, the theory Th(formulas)."""
    return frozenset(i for i, w in enumerate(ws) if all(evaluate(f, w) for f in formulas))


def generating_defaults(theory: Tuple[Sequence[Formula], Sequence[Default]], e: Models, ws) -> FrozenSet[int]:
    """The least set G of defaults such that each prerequisite follows from W and cons(G) and each justification is consistent with e."""
    w_part, d_part = theory
    g: FrozenSet[int] = frozenset()  # @line L4
    while True:  # @line L5
        th = models(list(w_part) + [d_part[i][2] for i in g], ws)
        g2 = frozenset(
            i for i, (alpha, betas, _) in enumerate(d_part)
            if th <= models([alpha], ws) and all(e & models([b], ws) for b in betas)
        )  # @line L6
        if g2 == g:  # @line L7
            return g
        g = g2  # @line L8


def gamma(theory, e: Models, ws) -> Models:
    """Reiter's operator: Gamma(e) = Th(W and the conclusions of the defaults that e allows)."""
    w_part, d_part = theory
    g = generating_defaults(theory, e, ws)
    return models(list(w_part) + [d_part[i][2] for i in g], ws)


def extensions(atom_names: Sequence[str], w_part: Sequence[Formula], d_part: Sequence[Default]) -> List[Tuple[Models, FrozenSet[int]]]:
    """All extensions of (W, D), each as (models of E, generating defaults), in a fixed order."""
    ws = worlds(atom_names)
    theory = (list(w_part), list(d_part))
    found: Dict[Models, FrozenSet[int]] = {}  # @line L1
    for k in range(len(d_part) + 1):  # @line L2
        for s in combinations(range(len(d_part)), k):
            e = models(list(w_part) + [d_part[i][2] for i in s], ws)  # @line L3
            if e in found:
                continue
            g = generating_defaults(theory, e, ws)
            if models(list(w_part) + [d_part[i][2] for i in g], ws) == e:  # @line L9
                found[e] = g
    return sorted(found.items(), key=lambda item: sorted(item[1]))  # @line L10


def in_extension(e: Models, phi: Formula, atom_names: Sequence[str]) -> bool:
    """Whether the extension with the models e contains phi."""
    ws = worlds(atom_names)
    return all(evaluate(phi, ws[i]) for i in e)
test_default_extension.py 169 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/default-extension/. The authors are listed in prov/DNSBMF.jsonl.

import random
import unittest
from itertools import combinations

from default_extension import evaluate, extensions, in_extension, worlds

TRUE = ("true",)
FALSE = ("false",)


def v(x):
    return ("var", x)


def neg(f):
    return ("not", f)


def disj(*fs):
    return ("or",) + fs


def conj(*fs):
    return ("and",) + fs


def normal(alpha, gamma):
    return (alpha, [gamma], gamma)


def reiter_fixed_points(names, w_part, d_part):
    """Every theory E over the atoms (a set of worlds) with E = union of Reiter's E_i (Theorem 2.1), by trying all of them."""
    ws = worlds(names)
    idx = range(len(ws))

    def mods(fs):
        return frozenset(i for i in idx if all(evaluate(f, ws[i]) for f in fs))

    out = set()
    for k in range(len(ws) + 1):
        for e in combinations(idx, k):
            e = frozenset(e)
            cur = mods(w_part)  # E_0 = Th(W)
            while True:
                add = [g for (a, bs, g) in d_part
                       if all(evaluate(a, ws[i]) for i in cur)
                       and all(any(evaluate(b, ws[i]) for i in e) for b in bs)]
                nxt = cur & mods(add)
                if nxt == cur:
                    break
                cur = nxt
            if cur == e:
                out.add(e)
    return out


def random_formula(rng, names, depth):
    if depth == 0 or rng.random() < 0.35:
        x = v(rng.choice(names))
        return neg(x) if rng.random() < 0.4 else x
    if rng.random() < 0.25:
        return neg(random_formula(rng, names, depth - 1))
    kids = [random_formula(rng, names, depth - 1) for _ in range(2)]
    return conj(*kids) if rng.random() < 0.5 else disj(*kids)


def random_theory(rng, names, normal_only=False):
    w_part = [random_formula(rng, names, 1) for _ in range(rng.randint(0, 2))]
    d_part = []
    for _ in range(rng.randint(0, 4)):
        alpha = TRUE if rng.random() < 0.4 else random_formula(rng, names, 1)
        gamma = random_formula(rng, names, 1)
        if normal_only:
            d_part.append(normal(alpha, gamma))
        else:
            betas = [random_formula(rng, names, 1) for _ in range(rng.randint(0, 2))]
            d_part.append((alpha, betas, gamma))
    return w_part, d_part


class TestExamples(unittest.TestCase):
    def test_nixon_diamond(self):
        names = ["q", "r", "p"]
        q, r, p = v("q"), v("r"), v("p")
        exts = extensions(names, [q, r], [normal(q, p), normal(r, neg(p))])
        self.assertEqual(len(exts), 2)
        (e1, g1), (e2, g2) = exts
        self.assertEqual((g1, g2), (frozenset({0}), frozenset({1})))
        self.assertTrue(in_extension(e1, p, names))
        self.assertTrue(in_extension(e2, neg(p), names))
        for e, _ in exts:
            self.assertTrue(in_extension(e, conj(q, r), names))

    def test_no_extension(self):
        a = v("a")
        self.assertEqual(extensions(["a"], [], [(TRUE, [neg(a)], a)]), [])
        # The same default next to a normal one still blocks every candidate.
        b = v("b")
        self.assertEqual(extensions(["a", "b"], [], [(TRUE, [neg(a)], a), normal(TRUE, b)]), [])

    def test_tweety(self):
        names = ["b", "f"]
        b, f = v("b"), v("f")
        exts = extensions(names, [b], [normal(b, f)])
        self.assertEqual(len(exts), 1)
        self.assertTrue(in_extension(exts[0][0], f, names))
        exts = extensions(names, [b, neg(f)], [normal(b, f)])
        self.assertEqual(exts[0][1], frozenset())
        self.assertFalse(in_extension(exts[0][0], f, names))

    def test_makinson_cumulativity(self):
        names = ["a", "b"]
        a, b = v("a"), v("b")
        d_part = [normal(TRUE, a), normal(disj(a, b), neg(a))]
        exts = extensions(names, [], d_part)
        self.assertEqual(len(exts), 1)
        self.assertTrue(in_extension(exts[0][0], a, names))
        self.assertTrue(in_extension(exts[0][0], disj(a, b), names))
        exts = extensions(names, [disj(a, b)], d_part)
        self.assertEqual(len(exts), 2)
        self.assertEqual([in_extension(e, a, names) for e, _ in exts], [True, False])
        self.assertTrue(in_extension(exts[1][0], conj(neg(a), b), names))

    def test_inconsistent_facts(self):
        a = v("a")
        exts = extensions(["a"], [a, neg(a)], [normal(TRUE, a)])
        self.assertEqual(exts, [(frozenset(), frozenset())])

    def test_no_defaults(self):
        a = v("a")
        exts = extensions(["a"], [a], [])
        self.assertEqual(len(exts), 1)
        self.assertTrue(in_extension(exts[0][0], a, ["a"]))


class TestAgainstFixedPoints(unittest.TestCase):
    def test_random_theories(self):
        rng = random.Random(1980)
        for t in range(500):
            names = ["a", "b"] if t % 10 else ["a", "b", "c"]
            w_part, d_part = random_theory(rng, names)
            got = {e for e, _ in extensions(names, w_part, d_part)}
            self.assertEqual(got, reiter_fixed_points(names, w_part, d_part), (w_part, d_part))


class TestNormalTheories(unittest.TestCase):
    def test_existence_orthogonality_and_semi_monotonicity(self):
        rng = random.Random(1987)
        names = ["a", "b", "c"]
        ws = worlds(names)
        for _ in range(300):
            w_part, d_part = random_theory(rng, names, normal_only=True)
            if not any(all(evaluate(f, w) for f in w_part) for w in ws):
                continue
            exts = [e for e, _ in extensions(names, w_part, d_part)]
            self.assertGreaterEqual(len(exts), 1)
            for e1, e2 in combinations(exts, 2):
                self.assertFalse(e1 & e2)  # distinct extensions contradict each other
            extra = normal(TRUE if rng.random() < 0.5 else random_formula(rng, names, 1), random_formula(rng, names, 1))
            bigger = [e for e, _ in extensions(names, w_part, d_part + [extra])]
            for e in exts:
                self.assertTrue(any(f <= e for f in bigger))  # some new extension contains e


if __name__ == "__main__":
    unittest.main()

References

  1. Raymond Reiter, A logic for default reasoning, Artificial Intelligence 13(1-2), 81 to 132 (1980), Section 2 (the operator Gamma, the definition of an extension and Theorem 2.1) and Section 3 (normal default theories always have extensions, semi-monotonicity, orthogonality)
  2. Sarit Kraus, Daniel Lehmann and Menachem Magidor, Nonmonotonic reasoning, preferential models and cumulative logics, Artificial Intelligence 44(1-2), 167 to 207 (1990), Section 3.1 (Makinson's observation that default logic, even with normal defaults, fails Cautious Monotonicity)
  3. Georg Gottlob, Complexity results for nonmonotonic logics, Journal of Logic and Computation 2(3), 397 to 425 (1992), the Sigma2P-completeness of deciding whether a finite propositional default theory has an extension

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/DNSBMF/text.yaml