mangle-analysis 0.7.0

Rust implementation of Mangle, a logic programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::PredicateSet;
use fxhash::{FxHashMap, FxHashSet};
use mangle_ast as ast;
use mangle_ast::Arena;
use std::fmt;

/// Represents a Mangle program consisting of logic rules and declarations.
///
/// `Program` wraps the AST and provides the necessary methods to identify
/// predicates and their dependencies. It is the primary input for the stratification algorithm.
///
/// It distinguishes between *extensional* predicates (stored facts) and *intensional*
/// predicates (derived rules).
#[derive(Clone)]
pub struct Program<'p> {
    pub arena: &'p Arena,
    pub ext_preds: Vec<ast::PredicateIndex>,
    pub rules: FxHashMap<ast::PredicateIndex, Vec<&'p ast::Clause<'p>>>,
}

impl<'p> fmt::Debug for Program<'p> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Program")
            .field("ext_preds", &self.ext_preds)
            .field("rules", &self.rules)
            .finish()
    }
}

/// A program that has been successfully stratified.
///
/// Contains the original program and the computed strata.
/// This structure is used to guide the execution order of the IR.
///
/// Stratification ensures that if a predicate `p` depends negatively on `q`,
/// then `q` is evaluated in an earlier stratum than `p`. This allows for
/// the correct evaluation of negation and aggregation (semi-naive evaluation).
#[derive(Clone)]
pub struct StratifiedProgram<'p> {
    program: Program<'p>,
    strata: Vec<PredicateSet>,
}

impl<'p> fmt::Debug for StratifiedProgram<'p> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StratifiedProgram")
            .field("program", &self.program)
            .field("strata", &self.strata)
            .finish()
    }
}

type EdgeMap = FxHashMap<ast::PredicateIndex, bool>;
type DepGraph = FxHashMap<ast::PredicateIndex, EdgeMap>;
type Nodeset = FxHashSet<ast::PredicateIndex>;

impl<'p> Program<'p> {
    pub fn new(arena: &'p Arena) -> Self {
        Self {
            arena,
            ext_preds: Vec::new(),
            rules: FxHashMap::default(),
        }
    }

    pub fn add_clause<'src>(&mut self, src: &'src Arena, clause: &'src ast::Clause) {
        let clause = self.arena.copy_clause(src, clause);
        let sym = clause.head.sym;
        use std::collections::hash_map::Entry;
        match self.rules.entry(sym) {
            Entry::Occupied(mut v) => v.get_mut().push(clause),
            Entry::Vacant(v) => {
                v.insert(vec![clause]);
            }
        }
    }

    /// Returns the AST Arena containing the program data.
    pub fn arena(&'p self) -> &'p ast::Arena {
        self.arena
    }

    /// Returns predicates for extensional DB (stored facts).
    pub fn extensional_preds(&'p self) -> PredicateSet {
        let mut set = FxHashSet::default();
        set.extend(&self.ext_preds);
        set
    }

    /// Returns predicates for intensional DB (derived rules).
    pub fn intensional_preds(&'p self) -> PredicateSet {
        let mut set = FxHashSet::default();
        set.extend(self.rules.keys());
        set
    }

    /// Maps predicates of intensional DB to their defining rules.
    pub fn rules(&'p self, sym: ast::PredicateIndex) -> impl Iterator<Item = &'p ast::Clause<'p>> {
        self.rules.get(&sym).unwrap().iter().copied()
    }

    /// Analyzes the program's dependency graph and attempts to stratify it.
    ///
    /// Stratification partitions the predicates into ordered layers (strata).
    /// This is essential for evaluating programs with negation or aggregation,
    /// ensuring that dependencies are fully evaluated before they are used.
    ///
    /// Returns a `StratifiedProgram` on success, or an error if the program
    /// contains unstratifiable cycles (e.g., negation cycles).
    pub fn stratify(self) -> Result<StratifiedProgram<'p>, String> {
        let dep = make_dep_graph(&self);
        let mut strata = dep.sccs();

        let mut pred_to_stratum: FxHashMap<ast::PredicateIndex, usize> = FxHashMap::default();

        for (i, c) in strata.iter().enumerate() {
            for sym in c {
                pred_to_stratum.insert(*sym, i);
            }
            for sym in c {
                if let Some(edges) = dep.get(sym) {
                    for (dest, negated) in edges {
                        if !*negated {
                            continue;
                        }
                        let dest_stratum = pred_to_stratum.get(dest);
                        if let Some(dest_stratum) = dest_stratum
                            && *dest_stratum == i
                        {
                            return Err("program cannot be stratified".to_string());
                        }
                    }
                }
            }
        }
        dep.sort_result(&mut strata, pred_to_stratum);
        let stratified = StratifiedProgram {
            program: self,
            strata: strata.into_iter().collect(),
        };
        Ok(stratified)
    }
}

impl<'p> StratifiedProgram<'p> {
    /// Returns the AST Arena containing the program data.
    pub fn arena(&'p self) -> &'p ast::Arena {
        self.program.arena()
    }

    /// Returns predicates for extensional DB (stored facts).
    pub fn extensional_preds(&'p self) -> PredicateSet {
        self.program.extensional_preds()
    }

    /// Returns predicates for intensional DB (derived rules).
    pub fn intensional_preds(&'p self) -> PredicateSet {
        self.program.intensional_preds()
    }

    /// Maps predicates of intensional DB to their defining rules.
    pub fn rules(&'p self, sym: ast::PredicateIndex) -> impl Iterator<Item = &'p ast::Clause<'p>> {
        self.program.rules(sym)
    }

    /// Returns an iterator of strata, in dependency order.
    /// Each stratum is a set of mutually recursive predicates that can be evaluated together.
    pub fn strata(&'p self) -> Vec<PredicateSet> {
        self.strata.to_vec()
    }

    /// Returns the stratum index for a given predicate symbol.
    /// Returns `None` if the predicate is not part of the stratified program (e.g. it's EDB).
    pub fn pred_to_index(&'p self, sym: ast::PredicateIndex) -> Option<usize> {
        self.strata.iter().position(|x| x.contains(&sym))
    }
}

fn make_dep_graph<'p>(program: &Program<'p>) -> DepGraph {
    let mut dep: DepGraph = FxHashMap::default();

    for (s, rule) in program.rules.iter() {
        dep.init_node(*s);
        for clause in rule.iter() {
            for premise in clause.premises.iter() {
                match premise {
                    ast::Term::Atom(atom_pred) => {
                        if !program.extensional_preds().contains(&atom_pred.sym) {
                            if clause.transform.is_empty() || clause.transform[0].var.is_some() {
                                dep.add_edge(*s, atom_pred.sym, false);
                            } else {
                                dep.add_edge(*s, atom_pred.sym, true);
                            }
                        }
                    }
                    ast::Term::NegAtom(atom_pred) => {
                        if !program.extensional_preds().contains(&atom_pred.sym) {
                            dep.add_edge(*s, atom_pred.sym, true);
                        }
                    }
                    ast::Term::TemporalAtom(atom_pred, _) => {
                        if !program.extensional_preds().contains(&atom_pred.sym) {
                            if clause.transform.is_empty() || clause.transform[0].var.is_some() {
                                dep.add_edge(*s, atom_pred.sym, false);
                            } else {
                                dep.add_edge(*s, atom_pred.sym, true);
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }
    dep
}

fn apply_permutation_cycle_rotate<T: Default>(arr: &mut [T], permutation: &[usize]) {
    let n = arr.len();
    if n == 0 {
        return;
    }
    let mut visited = vec![false; n];
    for i in 0..n {
        if !visited[i] {
            let mut current_idx = i;
            if permutation[current_idx] == i {
                visited[i] = true;
                continue;
            }
            let mut current_val = std::mem::take(&mut arr[i]);
            loop {
                let target_idx = permutation[current_idx];
                visited[current_idx] = true;
                let next_val = std::mem::replace(&mut arr[target_idx], current_val);
                current_val = next_val;
                current_idx = target_idx;
                if current_idx == i {
                    break;
                }
            }
        }
    }
}

trait DepGraphExt {
    fn init_node(&mut self, src: ast::PredicateIndex);
    fn add_edge(&mut self, src: ast::PredicateIndex, dest: ast::PredicateIndex, negated: bool);
    fn transpose(&self) -> DepGraph;
    fn sccs(&self) -> Vec<Nodeset>;
    fn sort_result(
        &self,
        strata: &mut Vec<Nodeset>,
        pred_to_stratum_map: FxHashMap<ast::PredicateIndex, usize>,
    ) -> FxHashMap<ast::PredicateIndex, usize>;
}

impl DepGraphExt for DepGraph {
    fn init_node(&mut self, src: ast::PredicateIndex) {
        self.entry(src).or_default();
    }

    fn add_edge(&mut self, src: ast::PredicateIndex, dest: ast::PredicateIndex, negated: bool) {
        let edges = self.entry(src).or_default();
        if negated {
            edges.insert(dest, negated);
            return;
        }
        if edges.get(&dest).is_none() || !edges[&dest] {
            edges.insert(dest, false);
        }
    }

    fn transpose(&self) -> DepGraph {
        let mut rev: DepGraph = FxHashMap::default();
        for (src, edges) in self.iter() {
            for (dest, negated) in edges.iter() {
                rev.init_node(*dest);
                rev.add_edge(*dest, *src, *negated);
            }
        }
        rev
    }

    fn sccs(&self) -> Vec<Nodeset> {
        let mut s: Vec<ast::PredicateIndex> = Vec::new();
        let mut seen: Nodeset = FxHashSet::default();

        fn visit(
            node: ast::PredicateIndex,
            graph: &DepGraph,
            s: &mut Vec<ast::PredicateIndex>,
            seen: &mut Nodeset,
        ) {
            if !seen.contains(&node) {
                seen.insert(node);
                if let Some(edges) = graph.get(&node) {
                    for &neighbor in edges.keys() {
                        visit(neighbor, graph, s, seen);
                    }
                }
                s.push(node);
            }
        }

        for (node, _) in self.iter() {
            visit(*node, self, &mut s, &mut seen);
        }

        let rev = self.transpose();
        let mut seen: Nodeset = FxHashSet::default();
        fn rvisit(
            node: ast::PredicateIndex,
            rev: &DepGraph,
            scc: &mut Nodeset,
            seen: &mut Nodeset,
        ) {
            if !seen.contains(&node) {
                seen.insert(node);
                scc.insert(node);
                if let Some(edges) = rev.get(&node) {
                    for &e in edges.keys() {
                        rvisit(e, rev, scc, seen);
                    }
                }
            }
        }
        let mut sccs: Vec<Nodeset> = Vec::new();
        while let Some(top) = s.pop() {
            if !seen.contains(&top) {
                let mut scc: Nodeset = FxHashSet::default();
                rvisit(top, &rev, &mut scc, &mut seen);
                if !scc.is_empty() {
                    sccs.push(scc);
                }
            }
        }
        sccs
    }

    fn sort_result(
        &self,
        strata: &mut Vec<Nodeset>,
        pred_to_stratum_map: FxHashMap<ast::PredicateIndex, usize>,
    ) -> FxHashMap<ast::PredicateIndex, usize> {
        let mut sorted_indices: Vec<usize> = Vec::new();
        let mut seen: FxHashSet<usize> = FxHashSet::default();
        let num_strata = strata.len();

        fn visit_stratum(
            index: usize,
            dep: &DepGraph,
            strata: &Vec<Nodeset>,
            pred_to_stratum_map: &FxHashMap<ast::PredicateIndex, usize>,
            seen: &mut FxHashSet<usize>,
            sorted_indices: &mut Vec<usize>,
        ) {
            if seen.contains(&index) {
                return;
            }
            seen.insert(index);

            if let Some(scc) = strata.get(index) {
                for sym in scc {
                    if let Some(edges) = dep.get(sym) {
                        for d in edges.keys() {
                            if let Some(&dep_stratum_index) = pred_to_stratum_map.get(d) {
                                visit_stratum(
                                    dep_stratum_index,
                                    dep,
                                    strata,
                                    pred_to_stratum_map,
                                    seen,
                                    sorted_indices,
                                );
                            }
                        }
                    }
                }
            }
            sorted_indices.push(index);
        }

        for i in 0..num_strata {
            visit_stratum(
                i,
                self,
                strata,
                &pred_to_stratum_map,
                &mut seen,
                &mut sorted_indices,
            );
        }

        let mut permutation = vec![0; num_strata];
        let mut old_to_new_map: FxHashMap<usize, usize> = FxHashMap::default();
        for new_idx in 0..num_strata {
            let old_idx = sorted_indices[new_idx];
            permutation[old_idx] = new_idx;
            old_to_new_map.insert(old_idx, new_idx);
        }

        apply_permutation_cycle_rotate(strata, &permutation);

        let mut new_pred_to_stratum_map: FxHashMap<ast::PredicateIndex, usize> =
            FxHashMap::default();
        for (sym, &old_idx) in pred_to_stratum_map.iter() {
            if let Some(&new_idx) = old_to_new_map.get(&old_idx) {
                new_pred_to_stratum_map.insert(*sym, new_idx);
            }
        }
        new_pred_to_stratum_map
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mangle_parse::Parser;

    #[test]
    fn test_stratification_success() {
        let arena = Arena::new_with_global_interner();
        let source = r#"
            p(1).
            q(X) :- p(X).
            r(X) :- q(X), !s(X).
            s(2).
        "#;
        let mut parser = Parser::new(&arena, source.as_bytes(), "test");
        parser.next_token().unwrap();
        let unit = parser.parse_unit().unwrap();

        let mut program = Program::new(&arena);
        for clause in unit.clauses {
            program.add_clause(&arena, clause);
        }

        let stratified = program.stratify().expect("should be stratifiable");

        // Helper to check relative order
        let get_stratum = |name: &str| -> Option<usize> {
            let name_idx = arena.lookup_opt(name)?;
            let pred_idx = arena.lookup_predicate_sym(name_idx)?;
            stratified.pred_to_index(pred_idx)
        };

        let s_idx = get_stratum("s");
        let r_idx = get_stratum("r");
        let q_idx = get_stratum("q");
        let p_idx = get_stratum("p");

        assert!(s_idx.is_some());
        assert!(r_idx.is_some());
        assert!(q_idx.is_some());
        assert!(p_idx.is_some());

        // r depends negatively on s, so r > s
        assert!(r_idx.unwrap() > s_idx.unwrap(), "r should be higher than s");

        // q depends on p, so q >= p
        assert!(q_idx.unwrap() >= p_idx.unwrap(), "q should be >= p");

        // r depends on q, so r >= q
        assert!(r_idx.unwrap() >= q_idx.unwrap(), "r should be >= q");
    }

    #[test]
    fn test_stratification_cycle() {
        let arena = Arena::new_with_global_interner();
        let source = "p(X) :- !p(X).";
        let mut parser = Parser::new(&arena, source.as_bytes(), "test");
        parser.next_token().unwrap();
        let unit = parser.parse_unit().unwrap();

        let mut program = Program::new(&arena);
        for clause in unit.clauses {
            program.add_clause(&arena, clause);
        }

        let res = program.stratify();
        assert!(res.is_err(), "should detect negation cycle");
    }
}