Algorithm Search⌘K

Algorithmadvanced

System Z

Also called Pearl's System Z, Z-ranking.

Ranks defaults by tolerance into layers and ranks worlds by the highest layer they violate, computing rational closure.

Time
O(r^2 · P) worst
Space
O(s) extra
Where
P = the cost of one satisfiability test on a formula of size O(s), exponential in the worst case; r = the number of conditionals; s = the total size of the conditionals
Properties
deterministic
Origin
Judea Pearl, Moisés Goldszmidt, 1990

System Z reasons with a finite set RR of propositional conditionals α→β\alpha \to \beta, read "if α\alpha then normally β\beta". It splits RR into layers by tolerance: the first layer holds the rules that can be satisfied without breaking any rule, the next layer holds the rules that can be satisfied without breaking the rules that remain, and so on. A world then gets the rank 1+1 + the highest layer of a rule that it breaks, or 00 if it breaks none. A query α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta is accepted when the best worlds of α\alpha satisfy β\beta. This ranking is the least ranking that respects every rule, and the relation that it defines is exactly the rational closure of Lehmann and Magidor. Every step is a satisfiability test, so the whole method is a polynomial number of SAT calls.

In plain words

You have rules of thumb such as "birds fly", "penguins are birds" and "penguins do not fly". Some rules can be followed in a situation that breaks no rule at all: a flying bird that is no penguin follows "birds fly" and breaks nothing. Those rules are the most general, and they go in the bottom layer. Other rules cannot be followed without breaking a general rule: any penguin breaks "birds fly". Those rules are more specific, and they go in a higher layer. A situation is as abnormal as the most specific rule that it breaks. To answer "do penguins fly?", look at the least abnormal penguins and see whether they fly. They do not, so the answer is no, and the specific rule wins over the general one.

The problem

Logic and semantics. The language is propositional logic over a finite set of atoms. A conditional is a pair α→β\alpha \to \beta of formulas. A ranking κ\kappa maps each world to a natural number or ∞\infty, with some world of rank 00, and gives a formula φ\varphi the rank κ(φ)=min⁡{κ(w):w⊨φ}\kappa(\varphi) = \min\{\kappa(w) : w \models \varphi\}, with min⁡∅=∞\min \emptyset = \infty. These are the ordinal conditional functions of Spohn. A ranking admits α→β\alpha \to \beta if κ(α∧β)<κ(α∧¬β)\kappa(\alpha \wedge \beta) < \kappa(\alpha \wedge \neg\beta): the best α\alpha worlds satisfy β\beta. RR is consistent if some ranking admits all its rules.

Task. Given RR and a query α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta, decide whether the query holds in System Z, that is, in the Z-ranking κZ\kappa_Z defined below. By the theorem of Goldszmidt and Pearl (1990), this is the same as deciding whether the query is in the rational closure of RR. The input is propositional. With R=∅R = \emptyset the query holds exactly when α⊨β\alpha \models \beta, so the problem is co-NP-hard. The algorithm below makes polynomially many calls of a SAT oracle, so the problem is in PNP\mathrm{P}^{\mathrm{NP}}, a class at the second level of the polynomial hierarchy. Eiter and Lukasiewicz (2000) studied the exact complexity of this problem and of related semantics, and the cases that are tractable. When the rules and the query are Horn, each test is a Horn-SAT test, and the whole decision takes polynomial time (Lehmann and Magidor 1992, Section 5.8).

The algorithm

A world verifies α→β\alpha \to \beta if it satisfies α∧β\alpha \wedge \beta and falsifies it if it satisfies α∧¬β\alpha \wedge \neg\beta. For a set SS of rules, M(S)M(S) is the conjunction of the material implications α⊃β\alpha \supset \beta over SS. A rule rr is tolerated by SS if some world verifies rr and falsifies nothing in SS, that is, if αr∧βr∧M(S)\alpha_r \wedge \beta_r \wedge M(S) is satisfiable.

  1. PARTITION(R) ▷ returns the layers R0, R1, ... and the rules that have no rank
  2. 1rest ← R; layers ← []
  3. 2while rest is not empty do
  4. 3layer ← {α → β ∈ rest : α ∧ β ∧ M(rest) is satisfiable} ▷ the rules that rest tolerates
  5. 4if layer is empty then return (layers, rest) ▷ R is not consistent
  6. 5append layer to layers
  7. 6rest ← rest ∖ layer
  8. 7return (layers, ∅)
  9. KAPPA(φ) ▷ k layers; R≥i is the union of layers i to k - 1 and rest
  10. 8if φ ∧ M(R≥k) is unsatisfiable then return ∞
  11. 9lo ← 0; hi ← k
  12. 10while lo < hi do
  13. 11mid ← ⌊(lo + hi) / 2⌋
  14. 12if φ ∧ M(R≥mid) is satisfiable then hi ← mid else lo ← mid + 1
  15. 13return lo
  16. ENTAILS(R, α, β)
  17. 14(layers, rest) ← PARTITION(R)
  18. 15a ← KAPPA(α ∧ β); b ← KAPPA(α ∧ ¬β)
  19. 16return a < b or b = ∞

The Z-rank of a rule in layer ii is Z(r)=iZ(r) = i. The Z-ranking of a world is κZ(w)=0\kappa_Z(w) = 0 if ww falsifies no rule, ∞\infty if it falsifies a rule without rank, and otherwise 1+max⁡{Z(r):w falsifies r}1 + \max\{Z(r) : w \text{ falsifies } r\}. KAPPA never lists the worlds. A world has rank at most ii exactly when it satisfies M(R≥i)M(R_{\ge i}), so κZ(φ)≤i\kappa_Z(\varphi) \le i exactly when φ∧M(R≥i)\varphi \wedge M(R_{\ge i}) is satisfiable. This test is monotone in ii, so a binary search finds the rank. The implementation decides satisfiability with the Tseitin transformation and a small DPLL solver.

Example

Take the atoms bb (bird), ff (flies) and pp (penguin), and the rules r1=b→fr_1 = b \to f, r2=p→br_2 = p \to b and r3=p→¬fr_3 = p \to \neg f.

  • Round 0, rest ={r1,r2,r3}= \{r_1, r_2, r_3\}. The world b,f,¬pb, f, \neg p verifies r1r_1 and falsifies nothing, so r1r_1 is tolerated. A world that verifies r2r_2 or r3r_3 is a penguin. If it flies it falsifies r3r_3, and if it is a bird that does not fly it falsifies r1r_1. So R0={r1}R_0 = \{r_1\}.
  • Round 1, rest ={r2,r3}= \{r_2, r_3\}. The world b,¬f,pb, \neg f, p verifies both and falsifies neither. So R1={r2,r3}R_1 = \{r_2, r_3\}, and the set is consistent.
world (b,f,p)(b, f, p) falsifies κZ\kappa_Z
any world with ¬p\neg p and not b∧¬fb \wedge \neg f nothing 0
b,¬f,¬pb, \neg f, \neg p r1r_1 1
b,¬f,pb, \neg f, p r1r_1 1
b,f,pb, f, p r3r_3 2
¬b,¬f,p\neg b, \neg f, p r2r_2 2
¬b,f,p\neg b, f, p r2,r3r_2, r_3 2

Then κZ(p∧¬f)=1<2=κZ(p∧f)\kappa_Z(p \wedge \neg f) = 1 < 2 = \kappa_Z(p \wedge f), so penguins normally do not fly, and κZ(p∧b)=1<2=κZ(p∧¬b)\kappa_Z(p \wedge b) = 1 < 2 = \kappa_Z(p \wedge \neg b), so penguins are normally birds. Also κZ(b∧¬p)=0<1=κZ(b∧p)\kappa_Z(b \wedge \neg p) = 0 < 1 = \kappa_Z(b \wedge p): birds are normally not penguins.

Add r4=b→wr_4 = b \to w (birds have wings). Now R0={r1,r4}R_0 = \{r_1, r_4\}. The best penguins break r1r_1, which has rank 00, whether or not they also break r4r_4. So κZ(p∧w)=κZ(p∧¬w)=1\kappa_Z(p \wedge w) = \kappa_Z(p \wedge \neg w) = 1, and System Z does not conclude that penguins have wings. This is the drowning problem: one broken rule of a layer hides all the other rules of that layer.

For the Nixon diamond, q→pq \to p and r→¬pr \to \neg p are tolerated at once and form one layer. The worlds with q∧rq \wedge r break one of the two rules, both with rank 00, so κZ(q∧r∧p)=κZ(q∧r∧¬p)=1\kappa_Z(q \wedge r \wedge p) = \kappa_Z(q \wedge r \wedge \neg p) = 1, and neither pp nor ¬p\neg p follows from q∧rq \wedge r.

Correctness

The correctness proof has the details. It shows four things. First, a world has rank at most ii exactly when it satisfies M(R≥i)M(R_{\ge i}), which justifies lines L8 to L13. Second, κZ\kappa_Z admits every rule: a rule of layer ii is verified by a world of rank at most ii and falsified only by worlds of rank at least i+1i + 1. Third, κZ\kappa_Z is the least admissible ranking: for any admissible κ′\kappa', an induction on ii shows that every world verifying a rule of R≥iR_{\ge i} has κ′\kappa' at least ii, because such a rule is not tolerated by R≥i−1R_{\ge i - 1} and so each such world breaks a rule of that set. The same induction shows that no ranking admits a set with rules left over at line L4. Fourth, a rule is tolerated by a set that contains it exactly when its antecedent is not exceptional in the sense of Lehmann and Magidor. So the layers are their sequence C0,C1,…C_0, C_1, \ldots of exceptional subsets, and line L16 gives the same answer as their decision procedure for rational closure.

Proof: System Z gives the least admissible ranking and decides rational closurefull

Let RR be a finite set of conditionals α→β\alpha \to \beta over a finite set of atoms, where α\alpha and β\beta are propositional formulas. A world is a truth assignment to the atoms. A world verifies the rule α→β\alpha \to \beta if it satisfies α∧β\alpha \wedge \beta, and falsifies it if it satisfies α∧¬β\alpha \wedge \neg\beta. For a set SS of rules, M(S)M(S) is the conjunction of the material implications α⊃β\alpha \supset \beta for the rules of SS, so a world satisfies M(S)M(S) exactly when it falsifies no rule of SS. A rule rr is tolerated by SS if some world verifies rr and falsifies no rule of SS, that is, if αr∧βr∧M(S)\alpha_r \wedge \beta_r \wedge M(S) is satisfiable.

The partition and the ranking

Lines L1 to L7 compute R≥0=RR_{\ge 0} = R and, for i=0,1,…i = 0, 1, \ldots, the layer RiR_i of the rules of R≥iR_{\ge i} that R≥iR_{\ge i} tolerates, with R≥i+1=R≥i∖RiR_{\ge i+1} = R_{\ge i} \setminus R_i. The loop stops at the first kk where R≥kR_{\ge k} is empty or tolerates none of its rules. Write R∞=R≥kR_\infty = R_{\ge k}. Each round removes at least one rule, so k≤∣R∣k \le |R|. The Z-rank of a rule r∈Rir \in R_i is Z(r)=iZ(r) = i. The set RR is consistent if R∞=∅R_\infty = \emptyset. Note that R≥i=Ri∪⋯∪Rk−1∪R∞R_{\ge i} = R_i \cup \cdots \cup R_{k-1} \cup R_\infty for 0≤i≤k0 \le i \le k.

The rank of a world ww is

κ(w)={∞if w falsifies a rule of R∞,0if w falsifies no rule,1+max⁡{Z(r):w falsifies r}otherwise,\kappa(w) = \begin{cases} \infty & \text{if } w \text{ falsifies a rule of } R_\infty, \\ 0 & \text{if } w \text{ falsifies no rule}, \\ 1 + \max\{Z(r) : w \text{ falsifies } r\} & \text{otherwise}, \end{cases}

and the rank of a formula is κ(φ)=min⁡{κ(w):w⊨φ}\kappa(\varphi) = \min\{\kappa(w) : w \models \varphi\}, with min⁡∅=∞\min \emptyset = \infty. System Z accepts α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta when κ(α∧β)<κ(α∧¬β)\kappa(\alpha \wedge \beta) < \kappa(\alpha \wedge \neg\beta) or κ(α∧¬β)=∞\kappa(\alpha \wedge \neg\beta) = \infty (line L16).

The rank of a formula with a satisfiability oracle

Lemma 1. For 0≤i≤k0 \le i \le k and every world ww: κ(w)≤i\kappa(w) \le i if and only if w⊨M(R≥i)w \models M(R_{\ge i}). Also, κ(w)<∞\kappa(w) < \infty if and only if w⊨M(R≥k)w \models M(R_{\ge k}).

Proof. For i=0i = 0: κ(w)=0\kappa(w) = 0 exactly when ww falsifies no rule, that is, w⊨M(R)w \models M(R). For 1≤i≤k1 \le i \le k: κ(w)≤i\kappa(w) \le i exactly when ww falsifies no rule of R∞R_\infty and every rule that it falsifies has Z(r)≤i−1Z(r) \le i - 1. These are the conditions that ww falsifies no rule of Ri,…,Rk−1,R∞R_i, \ldots, R_{k-1}, R_\infty, which is w⊨M(R≥i)w \models M(R_{\ge i}). For the last claim, κ(w)<∞\kappa(w) < \infty exactly when ww falsifies no rule of R∞=R≥kR_\infty = R_{\ge k}, and then κ(w)≤k\kappa(w) \le k because every Z-rank is at most k−1k - 1. □\square

Corollary 2. κ(φ)≤i\kappa(\varphi) \le i if and only if φ∧M(R≥i)\varphi \wedge M(R_{\ge i}) is satisfiable, for 0≤i≤k0 \le i \le k, and κ(φ)=∞\kappa(\varphi) = \infty if and only if φ∧M(R≥k)\varphi \wedge M(R_{\ge k}) is unsatisfiable.

So the predicate "φ∧M(R≥i)\varphi \wedge M(R_{\ge i}) is satisfiable" is false and then true as ii goes from 00 to kk. Line L8 handles the case κ(φ)=∞\kappa(\varphi) = \infty, and lines L9 to L13 find the least ii for which the predicate is true by binary search. That value is κ(φ)\kappa(\varphi).

The ranking is admissible and least

A ranking is a function κ′\kappa' from worlds to {0,1,2,…}∪{∞}\{0, 1, 2, \ldots\} \cup \{\infty\}, extended to formulas by the minimum as above. It admits the rule α→β\alpha \to \beta if κ′(α∧β)<κ′(α∧¬β)\kappa'(\alpha \wedge \beta) < \kappa'(\alpha \wedge \neg\beta). It is admissible for RR if it admits every rule of RR and gives some world rank 00.

Lemma 3. κ\kappa admits every rule that has a Z-rank. If RR is consistent, κ\kappa is admissible for RR.

Proof. Let r∈Rir \in R_i. Since R≥iR_{\ge i} tolerates rr, some world ww verifies rr and falsifies no rule of R≥iR_{\ge i}. By Lemma 1, κ(w)≤i\kappa(w) \le i, so κ(αr∧βr)≤i\kappa(\alpha_r \wedge \beta_r) \le i. Every world that falsifies rr has rank at least 1+i1 + i. So κ(αr∧βr)≤i<i+1≤κ(αr∧¬βr)\kappa(\alpha_r \wedge \beta_r) \le i < i + 1 \le \kappa(\alpha_r \wedge \neg\beta_r). If RR is consistent, every rule has a Z-rank. If RR is empty, every world has rank 00. Otherwise R0R_0 is not empty, so some world verifies a rule of R0R_0 and falsifies no rule of RR, and it has rank 00. □\square

Lemma 4. Let κ′\kappa' be a ranking that admits every rule of RR. Then for each i≤ki \le k and each rule r∈R≥ir \in R_{\ge i}, κ′(αr∧βr)≥i\kappa'(\alpha_r \wedge \beta_r) \ge i.

Proof. By induction on ii. The case i=0i = 0 is clear. Assume the claim for i<ki < k, and let r∈R≥i+1r \in R_{\ge i+1}. Then r∉Rir \notin R_i, so R≥iR_{\ge i} does not tolerate rr: every world ww that verifies rr falsifies some rule s∈R≥is \in R_{\ge i}. Then κ′(w)≥κ′(αs∧¬βs)>κ′(αs∧βs)≥i\kappa'(w) \ge \kappa'(\alpha_s \wedge \neg\beta_s) > \kappa'(\alpha_s \wedge \beta_s) \ge i, using that κ′\kappa' admits ss and the induction hypothesis. So κ′(w)≥i+1\kappa'(w) \ge i + 1 for every such ww, and κ′(αr∧βr)≥i+1\kappa'(\alpha_r \wedge \beta_r) \ge i + 1. □\square

Theorem 5 (least admissible ranking). If RR is consistent and κ′\kappa' is admissible for RR, then κ(w)≤κ′(w)\kappa(w) \le \kappa'(w) for every world ww. If RR is not consistent, no ranking admits every rule of RR.

Proof. Let RR be consistent. If ww falsifies no rule, κ(w)=0≤κ′(w)\kappa(w) = 0 \le \kappa'(w). Otherwise let rr be a rule that ww falsifies with the largest Z-rank ii. By Lemma 4, κ′(αr∧βr)≥i\kappa'(\alpha_r \wedge \beta_r) \ge i. Since κ′\kappa' admits rr, κ′(w)≥κ′(αr∧¬βr)≥i+1=κ(w)\kappa'(w) \ge \kappa'(\alpha_r \wedge \neg\beta_r) \ge i + 1 = \kappa(w).

Now let R∞≠∅R_\infty \ne \emptyset, and suppose that κ′\kappa' admits every rule. The set R∞R_\infty tolerates none of its rules. The argument of Lemma 4, applied inside R∞R_\infty, shows by induction on jj that κ′(αr∧βr)≥j\kappa'(\alpha_r \wedge \beta_r) \ge j for every r∈R∞r \in R_\infty and every jj: each world that verifies rr falsifies some s∈R∞s \in R_\infty and so has rank above κ′(αs∧βs)≥j\kappa'(\alpha_s \wedge \beta_s) \ge j. So κ′(αr∧βr)=∞\kappa'(\alpha_r \wedge \beta_r) = \infty, and κ′\kappa' cannot admit rr, because ∞<κ′(αr∧¬βr)\infty < \kappa'(\alpha_r \wedge \neg\beta_r) is false. □\square

So, for a consistent set, κ\kappa is the least admissible ranking, and the set has an admissible ranking exactly when it is consistent.

System Z is rational closure

We use the decision procedure of Lehmann and Magidor (1992, Section 5.8). For a finite set CC of conditionals, a formula α\alpha is exceptional for CC if CC preferentially entails ⊤∣ ⁣∼¬α\top \mathrel{|\!\sim} \neg\alpha, and by their Lemma 33 and Corollary 6 this holds exactly when M(C)⊨¬αM(C) \models \neg\alpha. Let E(C)E(C) be the set of the rules of CC whose antecedent is exceptional for CC. Their procedure decides whether α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta is in the rational closure of RR:

  1. C←RC \leftarrow R.
  2. While α\alpha is exceptional for CC and E(C)≠CE(C) \ne C, set C←E(C)C \leftarrow E(C).
  3. Answer yes if α∧¬β\alpha \wedge \neg\beta is exceptional for CC, and no otherwise.

Lemma 6. For every finite CC, a rule r∈Cr \in C is tolerated by CC if and only if αr\alpha_r is not exceptional for CC. So E(C)E(C) is the set of the rules of CC that CC does not tolerate, and the sequence C0=RC_0 = R, Ci+1=E(Ci)C_{i+1} = E(C_i) is Ci=R≥iC_i = R_{\ge i} for i≤ki \le k and Ci=R∞C_i = R_\infty for i≥ki \ge k.

Proof. Since r∈Cr \in C, M(C)M(C) contains αr⊃βr\alpha_r \supset \beta_r, so αr∧M(C)\alpha_r \wedge M(C) is satisfiable exactly when αr∧βr∧M(C)\alpha_r \wedge \beta_r \wedge M(C) is. The first is "not exceptional", the second is "tolerated". So E(Ci)=Ci∖{tolerated rules}E(C_i) = C_i \setminus \{\text{tolerated rules}\}. For i<ki < k this is R≥i∖Ri=R≥i+1R_{\ge i} \setminus R_i = R_{\ge i+1}. For C=R∞C = R_\infty no rule is tolerated, so E(R∞)=R∞E(R_\infty) = R_\infty. □\square

Theorem 7. For all formulas α\alpha and β\beta, System Z accepts α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta if and only if the procedure of Lehmann and Magidor answers yes, that is, if and only if α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta is in the rational closure of RR.

Proof. By Lemma 6, the procedure walks through R≥0,R≥1,…R_{\ge 0}, R_{\ge 1}, \ldots. It stops at the first i≤ki \le k where α\alpha is not exceptional for R≥iR_{\ge i}, or at the fixed point R≥kR_{\ge k}.

Case κ(α)<∞\kappa(\alpha) < \infty. By Corollary 2, α\alpha is not exceptional for R≥iR_{\ge i} exactly when κ(α)≤i\kappa(\alpha) \le i, so the procedure stops at i=κ(α)i = \kappa(\alpha). It answers yes exactly when α∧¬β∧M(R≥i)\alpha \wedge \neg\beta \wedge M(R_{\ge i}) is unsatisfiable, which by Corollary 2 means κ(α∧¬β)>i=κ(α)\kappa(\alpha \wedge \neg\beta) > i = \kappa(\alpha). Since κ(α)=min⁡(κ(α∧β),κ(α∧¬β))\kappa(\alpha) = \min(\kappa(\alpha \wedge \beta), \kappa(\alpha \wedge \neg\beta)), this holds exactly when κ(α∧β)<κ(α∧¬β)\kappa(\alpha \wedge \beta) < \kappa(\alpha \wedge \neg\beta). That is the test at line L16.

Case κ(α)=∞\kappa(\alpha) = \infty. Then α\alpha is exceptional for every R≥iR_{\ge i}, and the procedure stops at the fixed point C=R≥kC = R_{\ge k} with M(C)⊨¬αM(C) \models \neg\alpha. So M(C)⊨¬(α∧¬β)M(C) \models \neg(\alpha \wedge \neg\beta) and it answers yes. System Z also answers yes, because κ(α∧¬β)≥κ(α)=∞\kappa(\alpha \wedge \neg\beta) \ge \kappa(\alpha) = \infty. ■\blacksquare

Goldszmidt and Pearl (1990) proved that System Z and rational closure agree. The argument above reaches the same result through the decision procedure of Lehmann and Magidor, and it also covers sets that are not consistent: there, every formula whose rank is ∞\infty entails every formula, as in rational closure.

Cost

The partition makes at most ∣R∣|R| rounds, and round ii makes one satisfiability test for each rule of R≥iR_{\ge i}, so there are at most ∣R∣(∣R∣+1)/2|R|(|R| + 1)/2 tests. Each rank κ(φ)\kappa(\varphi) needs one test at line L8 and at most ⌈log⁡2(k+1)⌉\lceil \log_2(k + 1) \rceil tests in the binary search. One query α∣ ⁣∼β\alpha \mathrel{|\!\sim} \beta after the partition needs two ranks, so O(log⁡∣R∣)O(\log |R|) tests, each on a formula of size O(∣R∣⋅ℓ+∣α∣+∣β∣)O(|R| \cdot \ell + |\alpha| + |\beta|), where ℓ\ell is the largest size of a rule.

Complexity

Let r=∣R∣r = |R| and let ss be the total size of the rules. PARTITION runs at most rr rounds, because each round removes at least one rule, and round ii tests each remaining rule once. So it makes at most r(r+1)/2r(r + 1)/2 satisfiability tests, each on a formula of size O(s)O(s). After the partition, one query needs two calls of KAPPA, each with O(log⁡r)O(\log r) tests. The total time is O(r2⋅P)O(r^2 \cdot P), where PP is the cost of one test. The space outside the solver is O(s)O(s) for the formula and the layers. In the worst case PP is exponential, because SAT is NP-complete, but the number of oracle calls is polynomial. For Horn rules each test runs in linear time, so the whole method is polynomial.

Properties

  • Least admissible ranking. κZ(w)≤κ′(w)\kappa_Z(w) \le \kappa'(w) for every world ww and every ranking κ′\kappa' that admits all rules. The rules are believed as weakly as possible, which is Pearl's idea of default reasoning with minimal surprise.
  • Rational closure. The relation α∣ ⁣∼Zβ\alpha \mathrel{|\!\sim}_Z \beta is rational closure, so it satisfies the rules of System P and Rational Monotonicity.
  • Consistency test for free. The set has an admissible ranking exactly when PARTITION uses up all the rules.
  • Specificity. A rule whose antecedent is exceptional for the others gets a higher layer and wins over them, as in the penguin example.
  • Irrelevance. Atoms that occur in no rule do not change ranks: with only a→ba \to b, also a∧c∣ ⁣∼ba \wedge c \mathrel{|\!\sim} b.
  • Deterministic and syntax independent. The layers depend only on the models of the formulas, not on how the rules are written or ordered.

Variants and improvements

  • System Z+. Goldszmidt and Pearl gave each default a strength, and the ranking then uses the strengths in place of the layer numbers.
  • Lexicographic closure. Lehmann (1995) compares worlds by the number of broken rules in each layer, from the highest layer down. This solves the drowning problem in the example above: the penguin that keeps its wings breaks fewer rules of layer 00.
  • Maximum entropy. Goldszmidt, Morris and Pearl (1993) rank worlds by the limit of the maximum entropy distributions, which also counts broken rules.
  • Inconsistent sets. The implementation keeps the rules left over at line L4 and gives every world that breaks one of them the rank ∞\infty. Then every formula of rank ∞\infty entails every formula, which is what rational closure does for such sets.
  • Compiled ranks. The partition does not depend on the query. Compute it once, and answer each query with O(log⁡r)O(\log r) satisfiability tests.

When to use it

Use System Z when you have propositional rules of thumb with exceptions, you want answers that respect specificity, and you want a semantics with clear properties: it is rational closure, it satisfies System P and Rational Monotonicity, and it has a polynomial number of SAT calls. If you need rules in the same layer to keep working after one of them is broken, use the lexicographic closure or maximum entropy. If you need priorities among defaults that do not come from specificity, use System Z+ or a default logic with priorities.

Pitfalls

  • The drowning problem. One broken rule of a layer blocks all rules of that layer for the exceptional worlds. Do not expect penguins to inherit wings from birds.
  • Material implication is not the conditional. α→β\alpha \to \beta is not α⊃β\alpha \supset \beta. The material counterpart is used only inside the tolerance test and the rank test.
  • Tolerance uses the remaining rules. A rule of round ii must be tolerated by all rules that are still left, not by the whole set. Testing against the whole set at every round gives wrong layers.
  • Strict test at line L16. κZ(α∧β)≤κZ(α∧¬β)\kappa_Z(\alpha \wedge \beta) \le \kappa_Z(\alpha \wedge \neg\beta) would accept both β\beta and ¬β\neg\beta for the Nixon diamond.
  • Unsatisfiable antecedents. If α\alpha has rank ∞\infty, every β\beta follows. Without the case b=∞b = \infty at line L16, the test fails for such α\alpha.

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.

system_z.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/system-z/. The authors are listed in prov/8532GG.jsonl.
"""Pearl's System Z for a finite set of propositional conditionals.

A formula is a nested tuple: ("var", name), ("not", f), ("and", f, ...),
("or", f, ...), ("true",) or ("false",). The helpers var, neg, conj, disj and
implies build them. A conditional "if alpha then normally beta" is a pair
(alpha, beta).

partition(rules) returns (layers, rest). layers[i] is the list of the indices
of the rules with Z-rank i. rest holds the indices of the rules that are never
tolerated; it is empty exactly when the set is consistent in Pearl's sense.
kappa(phi, rules) is the Z-rank of a formula, with INF for a formula that no
ranked world satisfies. entails(rules, alpha, beta) decides alpha |~ beta in
System Z, which is the same relation as rational closure.

The only oracle is a satisfiability test: the Tseitin transformation followed
by a small DPLL solver.
"""

from typing import Dict, List, Optional, Tuple

Formula = tuple
Rule = Tuple[Formula, Formula]
Split = Tuple[List[List[int]], List[int]]

INF = float("inf")
TRUE = ("true",)
FALSE = ("false",)


def var(name):
    return ("var", name)


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


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


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


def implies(f, g):
    return disj(neg(f), g)


def evaluate(f, world):
    """The truth value of f in a world, given as a dict from atom names to booleans."""
    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 atoms(f, out=None):
    out = set() if out is None else out
    if f[0] == "var":
        out.add(f[1])
    else:
        for g in f[1:]:
            atoms(g, out)
    return out


# ---- the satisfiability oracle ----

def _tseitin(f):
    """Clauses over positive and negative ints that are satisfiable exactly when f is."""
    ids, clauses = {}, []

    def lit(g):
        op = g[0]
        if op == "var":
            key = ("var", g[1])
            if key not in ids:
                ids[key] = len(ids) + 1
            return ids[key]
        if op == "not":
            return -lit(g[1])
        x = len(ids) + 1
        ids[("gate", x)] = x
        if op in ("true", "false"):
            clauses.append([x] if op == "true" else [-x])
            return x
        kids = [lit(h) for h in g[1:]]
        if op == "and":
            for k in kids:
                clauses.append([-x, k])
            clauses.append([x] + [-k for k in kids])
        else:
            for k in kids:
                clauses.append([x, -k])
            clauses.append([-x] + kids)
        return x

    clauses.append([lit(f)])
    return clauses


def _dpll(clauses, assignment):
    while True:
        unit = None
        simplified = []
        for c in clauses:
            if any(assignment.get(abs(l)) == (l > 0) for l in c):
                continue
            rest = [l for l in c if abs(l) not in assignment]
            if not rest:
                return False
            if len(rest) == 1 and unit is None:
                unit = rest[0]
            simplified.append(rest)
        clauses = simplified
        if not clauses:
            return True
        if unit is None:
            break
        assignment[abs(unit)] = unit > 0
    pick = clauses[0][0]
    for value in (pick > 0, pick < 0):
        trial = dict(assignment)
        trial[abs(pick)] = value
        if _dpll(clauses, trial):
            return True
    return False


def satisfiable(f: Formula) -> bool:
    """Whether some world satisfies f. Exponential time in the worst case."""
    return _dpll(_tseitin(f), {})


# ---- System Z ----

def material(rule):
    """The material counterpart alpha -> beta of a conditional."""
    return implies(rule[0], rule[1])


def partition(rules: List[Rule]) -> Split:
    """Pearl's tolerance partition. Returns (layers, rest) as lists of rule indices.

    Cost: at most r (r + 1) / 2 satisfiability tests for r rules.
    """
    rest, layers = list(range(len(rules))), []  # @line L1
    while rest:  # @line L2
        m = [material(rules[j]) for j in rest]
        layer = [i for i in rest if satisfiable(conj(rules[i][0], rules[i][1], *m))]  # @line L3
        if not layer:  # @line L4
            return layers, rest
        layers.append(layer)  # @line L5
        rest = [i for i in rest if i not in layer]  # @line L6
    return layers, []  # @line L7


def z_ranks(rules: List[Rule]) -> Dict[int, int]:
    """A dict from rule index to its Z-rank, for the rules that have one."""
    layers, _ = partition(rules)
    return {i: z for z, layer in enumerate(layers) for i in layer}


def _above(rules, layers, rest, i):
    """The material counterparts of the rules in rest and in the layers from i on."""
    chosen = list(rest) + [j for layer in layers[i:] for j in layer]
    return [material(rules[j]) for j in chosen]


def kappa(phi: Formula, rules: List[Rule], split: Optional[Split] = None) -> float:
    """The Z-rank of the formula phi: the least rank of a world that satisfies it.

    Cost after the partition: one test plus a binary search over the layers.
    """
    layers, rest = partition(rules) if split is None else split
    if not satisfiable(conj(phi, *_above(rules, layers, rest, len(layers)))):  # @line L8
        return INF
    lo, hi = 0, len(layers)  # @line L9
    while lo < hi:  # @line L10
        mid = (lo + hi) // 2  # @line L11
        if satisfiable(conj(phi, *_above(rules, layers, rest, mid))):  # @line L12
            hi = mid
        else:
            lo = mid + 1
    return lo  # @line L13


def rank_world(world: Dict[str, bool], rules: List[Rule], split: Optional[Split] = None) -> float:
    """The Z-rank of one world: 0 if it falsifies no rule, else 1 + the largest Z-rank it falsifies."""
    layers, rest = partition(rules) if split is None else split
    falsified = [i for i, (a, b) in enumerate(rules) if evaluate(a, world) and not evaluate(b, world)]
    if any(i in rest for i in falsified):
        return INF
    z = {i: k for k, layer in enumerate(layers) for i in layer}
    return 1 + max(z[i] for i in falsified) if falsified else 0


def entails(rules: List[Rule], alpha: Formula, beta: Formula, split: Optional[Split] = None) -> bool:
    """alpha |~ beta in System Z, which is rational closure."""
    split = partition(rules) if split is None else split  # @line L14
    a = kappa(conj(alpha, beta), rules, split)  # @line L15
    b = kappa(conj(alpha, neg(beta)), rules, split)
    return a < b or b == INF  # @line L16
test_system_z.py 220 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/system-z/. The authors are listed in prov/8532GG.jsonl.

import random
import unittest
from itertools import product

from system_z import (
    FALSE,
    INF,
    TRUE,
    conj,
    disj,
    entails,
    evaluate,
    kappa,
    neg,
    partition,
    rank_world,
    satisfiable,
    var,
    z_ranks,
)

a, b, c, d = var("a"), var("b"), var("c"), var("d")


def worlds(names):
    return [dict(zip(names, v)) for v in product([False, True], repeat=len(names))]


def holds(rule, w):
    return not evaluate(rule[0], w) or evaluate(rule[1], w)


def exceptional_bf(alpha, rules, ws):
    """Lehmann and Magidor: alpha is exceptional for C when no world satisfies alpha and every material counterpart in C."""
    return not any(evaluate(alpha, w) and all(holds(r, w) for r in rules) for w in ws)


def partition_bf(rules, ws):
    rest, layers = list(range(len(rules))), []
    while rest:
        layer = [
            i for i in rest
            if any(evaluate(rules[i][0], w) and evaluate(rules[i][1], w) and all(holds(rules[j], w) for j in rest) for w in ws)
        ]
        if not layer:
            return layers, rest
        layers.append(layer)
        rest = [i for i in rest if i not in layer]
    return layers, []


def world_rank_bf(rules, split, w):
    layers, rest = split
    z = {i: k for k, layer in enumerate(layers) for i in layer}
    bad = [i for i, r in enumerate(rules) if not holds(r, w)]
    if any(i in rest for i in bad):
        return INF
    return 1 + max(z[i] for i in bad) if bad else 0


def kappa_bf(rules, split, phi, ws):
    return min((world_rank_bf(rules, split, w) for w in ws if evaluate(phi, w)), default=INF)


def rational_closure_bf(rules, alpha, beta, ws):
    """The decision procedure of Lehmann and Magidor (1992, Section 5.8), with exceptionality decided by enumeration."""
    cur = list(rules)
    while exceptional_bf(alpha, cur, ws):
        nxt = [r for r in cur if exceptional_bf(r[0], cur, ws)]
        if nxt == cur:
            break
        cur = nxt
    return exceptional_bf(conj(alpha, neg(beta)), cur, ws)


def random_formula(rng, names, depth):
    if depth == 0 or rng.random() < 0.3:
        x = var(rng.choice(names))
        r = rng.random()
        if r < 0.05:
            return TRUE
        if r < 0.08:
            return FALSE
        return neg(x) if rng.random() < 0.4 else x
    op = rng.choice(["and", "or", "not"])
    if op == "not":
        return neg(random_formula(rng, names, depth - 1))
    kids = [random_formula(rng, names, depth - 1) for _ in range(rng.randint(2, 3))]
    return conj(*kids) if op == "and" else disj(*kids)


def random_rules(rng, names):
    return [
        (random_formula(rng, names, rng.randint(0, 2)), random_formula(rng, names, rng.randint(0, 2)))
        for _ in range(rng.randint(1, 5))
    ]


class TestOracle(unittest.TestCase):
    def test_satisfiable_matches_truth_tables(self):
        rng = random.Random(2)
        names = ["a", "b", "c", "d"]
        ws = worlds(names)
        for _ in range(800):
            f = random_formula(rng, names, 3)
            self.assertEqual(satisfiable(f), any(evaluate(f, w) for w in ws), f)
        self.assertTrue(satisfiable(TRUE))
        self.assertFalse(satisfiable(FALSE))
        self.assertFalse(satisfiable(conj(a, neg(a))))


class TestAgainstBruteForce(unittest.TestCase):
    def test_partition_ranks_and_rational_closure(self):
        rng = random.Random(1990)
        for _ in range(150):
            names = ["a", "b", "c", "d"][: rng.randint(1, 4)]
            ws = worlds(names)
            rules = random_rules(rng, names)
            split = partition(rules)
            self.assertEqual(split, partition_bf(rules, ws), rules)
            for w in ws:
                self.assertEqual(rank_world(w, rules, split), world_rank_bf(rules, split, w))
            for _ in range(6):
                alpha = random_formula(rng, names, 2)
                beta = random_formula(rng, names, 2)
                self.assertEqual(kappa(alpha, rules, split), kappa_bf(rules, split, alpha, ws))
                self.assertEqual(
                    entails(rules, alpha, beta, split),
                    rational_closure_bf(rules, alpha, beta, ws),
                    (rules, alpha, beta),
                )
            for rule in rules:  # every rule is entailed by its own set
                self.assertTrue(entails(rules, rule[0], rule[1], split))

    def test_least_admissible_ranking(self):
        # Over two atoms, try every ranking with values 0 to 3 that gives some world rank 0.
        rng = random.Random(1992)
        names = ["a", "b"]
        ws = worlds(names)
        all_rankings = [r for r in product(range(4), repeat=len(ws)) if 0 in r]

        def rank_of(ranking, phi):
            return min((ranking[k] for k, w in enumerate(ws) if evaluate(phi, w)), default=INF)

        def admissible(ranking, rules):
            return all(rank_of(ranking, conj(x, y)) < rank_of(ranking, conj(x, neg(y))) for x, y in rules)

        checked = 0
        while checked < 25:
            rules = random_rules(rng, names)
            split = partition(rules)
            if split[1]:
                self.assertFalse(any(admissible(r, rules) for r in all_rankings))
                continue
            checked += 1
            kz = tuple(rank_world(w, rules, split) for w in ws)
            self.assertTrue(admissible(kz, rules))
            for r in all_rankings:
                if admissible(r, rules):
                    self.assertTrue(all(x <= y for x, y in zip(kz, r)), (rules, r))


class TestExamples(unittest.TestCase):
    def setUp(self):
        self.bird, self.fly, self.penguin, self.wings = var("b"), var("f"), var("p"), var("w")

    def test_penguin(self):
        b_, f_, p_ = self.bird, self.fly, self.penguin
        rules = [(b_, f_), (p_, b_), (p_, neg(f_))]
        self.assertEqual(partition(rules), ([[0], [1, 2]], []))
        self.assertEqual(z_ranks(rules), {0: 0, 1: 1, 2: 1})
        self.assertTrue(entails(rules, b_, f_))
        self.assertTrue(entails(rules, p_, neg(f_)))
        self.assertTrue(entails(rules, p_, b_))
        self.assertTrue(entails(rules, conj(b_, neg(p_)), f_))
        self.assertFalse(entails(rules, p_, f_))
        self.assertEqual(kappa(p_, rules), 1)
        self.assertEqual(kappa(b_, rules), 0)
        self.assertTrue(entails(rules, b_, neg(p_)))  # birds are normally not penguins

    def test_drowning(self):
        b_, f_, p_, w_ = self.bird, self.fly, self.penguin, self.wings
        rules = [(b_, f_), (p_, b_), (p_, neg(f_)), (b_, w_)]
        self.assertEqual(partition(rules)[0], [[0, 3], [1, 2]])
        self.assertTrue(entails(rules, b_, w_))
        self.assertFalse(entails(rules, p_, w_))
        self.assertFalse(entails(rules, p_, neg(w_)))

    def test_nixon(self):
        q, r, p = var("q"), var("r"), var("pacifist")
        rules = [(q, p), (r, neg(p))]
        self.assertEqual(partition(rules), ([[0, 1]], []))
        self.assertFalse(entails(rules, conj(q, r), p))
        self.assertFalse(entails(rules, conj(q, r), neg(p)))
        self.assertTrue(entails(rules, q, p))

    def test_irrelevance(self):
        rules = [(a, b)]
        self.assertTrue(entails(rules, conj(a, c), b))

    def test_inconsistent_sets(self):
        for rules in ([(a, b), (a, neg(b))], [(TRUE, a), (TRUE, neg(a))], [(a, FALSE)]):
            layers, rest = partition(rules)
            self.assertTrue(rest)
            self.assertEqual(kappa(a, rules), INF)
            self.assertTrue(entails(rules, a, c))
        self.assertEqual(partition([(a, b), (a, neg(b)), (c, d)]), ([[2]], [0, 1]))

    def test_empty_set(self):
        self.assertEqual(partition([]), ([], []))
        self.assertTrue(entails([], a, a))
        self.assertFalse(entails([], a, b))
        self.assertTrue(entails([], FALSE, b))


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

History

Adams' probabilistic semantics for conditionals gave the notion of a consistent set of defaults. Pearl (1990) turned the consistency test into an ordering of the defaults, defined the ranking of worlds from it, and called the result System Z. In the same year Goldszmidt and Pearl showed that System Z agrees with the rational closure that Lehmann and Magidor had defined from a different starting point, the KLM rules and ranked models; the journal version of Lehmann and Magidor appeared in 1992. Later work on maximum entropy (1993), the lexicographic closure (1995) and System Z+ addressed the drowning problem and defaults of different strengths. Eiter and Lukasiewicz (2000) mapped the complexity of these semantics.

References

  1. Judea Pearl, System Z: a natural ordering of defaults with tractable applications to nonmonotonic reasoning, in Rohit Parikh (ed.), Proceedings of the Third Conference on Theoretical Aspects of Reasoning about Knowledge (TARK 1990), Morgan Kaufmann, 121 to 135 (1990), tolerance, the partition of the defaults, the ranking of worlds and the entailment relation
  2. Moisés Goldszmidt and Judea Pearl, On the relation between rational closure and System Z, Proceedings of the Third International Workshop on Nonmonotonic Reasoning, South Lake Tahoe, 130 to 140 (1990), the proof that System Z entailment and rational closure coincide
  3. Daniel Lehmann and Menachem Magidor, What does a conditional knowledge base entail?, Artificial Intelligence 55(1), 1 to 60 (1992), Section 2.6 (exceptional formulas), Lemma 33 and Corollary 6 (exceptionality by the material counterpart), Section 5.8 (the decision procedure for rational closure)
  4. Moisés Goldszmidt and Judea Pearl, Qualitative probabilities for default reasoning, belief revision, and causal modeling, Artificial Intelligence 84(1-2), 57 to 112 (1996), ranking functions for default reasoning, and System Z+ with strengths on the defaults
  5. Thomas Eiter and Thomas Lukasiewicz, Default reasoning from conditional knowledge bases, complexity and tractable cases, Artificial Intelligence 124(2), 169 to 241 (2000), the complexity of entailment in System Z and related semantics, and the tractable cases
  6. Daniel Lehmann, Another perspective on default reasoning, Annals of Mathematics and Artificial Intelligence 15(1), 61 to 82 (1995), the lexicographic closure
  7. Moisés Goldszmidt, Paul Morris and Judea Pearl, A maximum entropy approach to nonmonotonic reasoning, IEEE Transactions on Pattern Analysis and Machine Intelligence 15(3), 220 to 232 (1993), the maximum entropy ranking of defaults

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 proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
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)
WritingW2 basic
  • a scene preset for the example