ASP-09 | Circular Dependencies
ASP handles circular dependencies in rules through external support requirements. This approach mirrors real-world relationship modeling where cycles need external validation.Photo Credit: Rob Grzywinski
Cycles in Rules
Look at the program below and think about what the result might be:p ← q .
q ← p .
Given that the answer set can at most contain the heads of the rules, there are four possibilities for the answer set:{ } (the empty set){ p }{ q }{ p q }
An answer set is valid if it satisfies all rules of the program. Let's look at each possible answer set in turn and determine if it violates any rule and is therefore disqualified as a candidate.{ } The empty set contains neither p nor q. Because there is no q, the first rule is removed due to the closed world assumption (see Logic Programming Rules! > Answer Sets and Grounding) which states that what is not currently known to be true, is false. (If the body of a rule evaluates to ⊥ then that rule is removed.) The second rule is also removed for the same reason. All rules are satisfied (specifically, they're all removed so there are no rules to satisfy) therefore the empty set is a valid answer set.{ p } As in the empty set case above, because there is no q, the first rule is removed. The second rule states that if p exists in the answer set then q must exist in the answer set. This answer set does not contain q therefore the rule is violated; is not a valid answer set.{ q } This answer set follows the same logic as { p } but with p and q transposed therefore it too is not a valid answer set.{ p q } The first rule states that if q is in the answer set then p must be in the answer set; this rule is satisfied. Similarly, the second rule states that if p is in the answer set then q must be in the answer set; this too is satisfied. Although no rule is violated, there's nothing to "prime the pump" so to speak (referred to as "externally supported" in the literature). There is no fact or rule that contributes either p or q such that the rules are satisfied therefore due to the closed world assumption ("we only know what we know"), this cannot be a valid answer set for these two rules.
The result of this program is the empty set ().(For the curious reader: Remember that answer sets must be minimal and the empty set is a subset of all other answer sets. Once it was determined that the empty set was an answer set, the other candidates did not need to be examined since they would not be minimal.)If you recall the first introduction to answer sets, we defined an answer set as "all atoms that can be (acyclically) derived from the rules of the program". The example above demonstrates why the notion of "acyclic" is an important addition to that definition. Rules such as above which are cyclic do not by themselves contribute atoms to the answer set; they must be made acyclic. A good way to remember this is that cyclic rules must be externally supported — some rule external from the cycle must “support” the cycle.Priming the Pump
If we add an additional fact to "prime the pump" (i.e. to make the rules acyclic — to externally support them)p .
then the result is as expected. The same is true if (2) stated q. (The empty set can no longer be a candidate since facts must be present in all answer sets.)Extending the Example
p ← q .
q ← r .
r ← p .
The result is the same as above:- If no atom is contributed outside of (externally supports) the cycle then the result is the empty set.
- If any atom is contributed outside of the cycle then the answer set contains all atoms.
(3) by itself has the answer set . (2) and (3) together have the answer set .
Real-World Example: Mutual Support
Consider a scenario modeling mutual support relationships between family members:supports(pete, mary) ← needs_help(mary), can_help(pete) .
supports(mary, pete) ← needs_help(pete), can_help(mary) .
needs_help(X) ← supports(Y, X) .
can_help(X) ← supports(X, Y) .
This creates a cycle: if Pete supports Mary, then Mary needs help, and if Mary needs help and Pete can help, Pete supports Mary. Without any external facts, we get the empty set () — the solver can't just assume support relationships exist.But if we add an external fact:supports(pete, mary) . % Pete initially offers support to Mary
Then we get a fuller picture of mutual support: .This mirrors real life — support relationships often need some initial action to get started. They can't just spring from nowhere, even if the potential for mutual support exists.The example shows how ASP's handling of cycles prevents us from getting into unrealistic situations where support relationships magically appear without cause, while still allowing us to model the reciprocal nature of family support once it's initiated.
Takeaways
The handling of circular dependencies in ASP reveals some fundamental principles:- External Support Required: Cycles don't create atoms "out of nothing". They need external support through facts or non-cyclic rules.
- Minimality Matters: The empty set, being minimal and satisfying cyclic rules, often wins out unless external support exists.
- Real World Modeling: This behavior isn't just a technical detail. It helps create more realistic models where relationships can't spontaneously emerge without cause.
In the next sections, we'll build on these concepts to handle even more complex relationships and constraints. Understanding how ASP handles cycles is crucial for writing effective programs that accurately model real-world dependencies and relationships.