Stratify checks whether a program can be stratified. It returns strongly-connected components and a map from predicate to stratum in the affirmative case, an error otherwise. The result list of strata is topologically sorted. Stratification means separating a list of intensional predicate symbols wi
(program Program)
| 77 | // into strata (layers) such that each layer only depends on the |
| 78 | // evaluation of negated atoms for idb predicates in lower layers. |
| 79 | func Stratify(program Program) ([]Nodeset, map[ast.PredicateSym]int, error) { |
| 80 | dep := makeDepGraph(program) |
| 81 | strata := dep.sccs() |
| 82 | predToStratum := make(map[ast.PredicateSym]int) |
| 83 | for i, c := range strata { |
| 84 | for sym := range c { |
| 85 | predToStratum[sym] = i |
| 86 | } |
| 87 | for sym := range c { |
| 88 | for dest, negated := range dep[sym] { |
| 89 | if !negated { |
| 90 | continue |
| 91 | } |
| 92 | // "Negative" dependency in same stratum indicates recursion through negation. |
| 93 | if destStratum, ok := predToStratum[dest]; ok && destStratum == i { |
| 94 | return nil, nil, fmt.Errorf("program cannot be stratified") |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | strata, predToStratum = dep.sortResult(strata, predToStratum) |
| 100 | return strata, predToStratum, nil |
| 101 | } |
| 102 | |
| 103 | func (dep depGraph) initNode(src ast.PredicateSym) { |
| 104 | if _, ok := dep[src]; !ok { |