kizzasi-logic 0.2.1

TensorLogic bridge for Kizzasi - constraint enforcement and safety guardrails
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Datalog-Style Logic Programming Integration
//!
//! A lightweight Datalog-style engine for constraint reasoning over signal facts.
//! Supports bottom-up fixpoint evaluation, unification, and a bridge for asserting
//! signal constraint facts into the engine.
//!
//! # Example
//!
//! ```
//! use kizzasi_logic::logic_prog::{Atom, Term, Rule, DatalogEngine};
//!
//! let mut engine = DatalogEngine::new();
//! engine.add_fact(Atom { predicate: "parent".to_string(), args: vec![Term::sym("tom"), Term::sym("bob")] });
//! engine.add_rule(Rule::new(
//!     Atom { predicate: "ancestor".to_string(), args: vec![Term::var("X"), Term::var("Y")] },
//!     vec![Atom { predicate: "parent".to_string(), args: vec![Term::var("X"), Term::var("Y")] }],
//! ));
//! let derived = engine.evaluate().unwrap();
//! assert!(engine.entails(&derived, &Atom { predicate: "ancestor".to_string(), args: vec![Term::sym("tom"), Term::sym("bob")] }));
//! ```

use crate::error::{LogicError, LogicResult};
use std::collections::{HashMap, HashSet};

// ============================================================================
// Term and Atom
// ============================================================================

/// A term in a logic atom: either a variable, a symbol, or an integer.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Term {
    /// A logic variable (by convention, starts with a capital letter)
    Var(String),
    /// A ground symbol / string literal
    Sym(String),
    /// An integer constant
    Int(i64),
}

impl Term {
    /// Return `true` iff this term is ground (i.e., not a variable).
    pub fn is_ground(&self) -> bool {
        !matches!(self, Term::Var(_))
    }

    /// Construct a `Var` term.
    pub fn var(name: &str) -> Self {
        Term::Var(name.to_string())
    }

    /// Construct a `Sym` term.
    pub fn sym(name: &str) -> Self {
        Term::Sym(name.to_string())
    }

    /// Construct an `Int` term.
    pub fn int(v: i64) -> Self {
        Term::Int(v)
    }
}

/// A ground-or-partially-ground atom: a predicate applied to a list of terms.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Atom {
    /// Predicate name
    pub predicate: String,
    /// Argument list (may contain variables before substitution)
    pub args: Vec<Term>,
}

impl Atom {
    /// Return `true` iff all arguments are ground.
    pub fn is_ground(&self) -> bool {
        self.args.iter().all(Term::is_ground)
    }
}

// ============================================================================
// Rule
// ============================================================================

/// A Datalog rule: `head :- body[0], body[1], ...`
///
/// A rule with an empty body is a fact (always derivable).
#[derive(Debug, Clone)]
pub struct Rule {
    /// The atom to derive when the body is satisfied
    pub head: Atom,
    /// Body atoms that must all be satisfied (existential join)
    pub body: Vec<Atom>,
}

impl Rule {
    /// Create a rule with a non-empty body.
    pub fn new(head: Atom, body: Vec<Atom>) -> Self {
        Self { head, body }
    }

    /// Create a unit fact (rule with empty body).
    pub fn fact(head: Atom) -> Self {
        Self { head, body: vec![] }
    }
}

// ============================================================================
// Substitution type
// ============================================================================

/// A variable-to-term mapping produced by unification.
pub type Substitution = HashMap<String, Term>;

// ============================================================================
// Unification
// ============================================================================

/// Apply a substitution to a single term, recursively chasing variable bindings.
fn apply_term(term: &Term, subst: &Substitution) -> Term {
    match term {
        Term::Var(name) => {
            if let Some(bound) = subst.get(name) {
                apply_term(bound, subst)
            } else {
                term.clone()
            }
        }
        other => other.clone(),
    }
}

/// Apply a substitution to an atom, producing a new atom with all variables replaced.
pub fn apply_subst(atom: &Atom, subst: &Substitution) -> Atom {
    Atom {
        predicate: atom.predicate.clone(),
        args: atom.args.iter().map(|t| apply_term(t, subst)).collect(),
    }
}

/// Attempt to unify atom `a` (possibly with variables) against ground atom `b`,
/// extending `subst`. Returns `Some(new_subst)` on success, `None` on failure.
pub fn unify(a: &Atom, b: &Atom, subst: &Substitution) -> Option<Substitution> {
    if a.predicate != b.predicate || a.args.len() != b.args.len() {
        return None;
    }

    let mut s = subst.clone();

    for (ta, tb) in a.args.iter().zip(b.args.iter()) {
        let ta_applied = apply_term(ta, &s);
        let tb_applied = apply_term(tb, &s);

        match (&ta_applied, &tb_applied) {
            (Term::Var(name), other) => {
                // Bind variable to ground term
                s.insert(name.clone(), other.clone());
            }
            (other, Term::Var(name)) => {
                // Bind variable to ground term (symmetric)
                s.insert(name.clone(), other.clone());
            }
            (a_term, b_term) => {
                if a_term != b_term {
                    return None;
                }
            }
        }
    }

    Some(s)
}

// ============================================================================
// DatalogEngine
// ============================================================================

/// Bottom-up Datalog evaluation engine.
///
/// Facts are asserted directly; rules are applied iteratively until a fixpoint
/// is reached (or `max_iterations` is exceeded). All derived facts (including
/// base facts) are returned from [`DatalogEngine::evaluate`].
pub struct DatalogEngine {
    facts: HashSet<Atom>,
    rules: Vec<Rule>,
    max_iterations: usize,
    last_iterations: std::cell::Cell<usize>,
}

impl Default for DatalogEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl DatalogEngine {
    /// Create a new engine with a default maximum of 1000 fixpoint iterations.
    pub fn new() -> Self {
        Self {
            facts: HashSet::new(),
            rules: Vec::new(),
            max_iterations: 1000,
            last_iterations: std::cell::Cell::new(0),
        }
    }

    /// Create an engine with a custom iteration limit.
    pub fn with_max_iterations(max_iter: usize) -> Self {
        Self {
            facts: HashSet::new(),
            rules: Vec::new(),
            max_iterations: max_iter,
            last_iterations: std::cell::Cell::new(0),
        }
    }

    /// Assert a base fact into the engine.
    pub fn add_fact(&mut self, atom: Atom) {
        self.facts.insert(atom);
    }

    /// Add a derivation rule to the engine.
    pub fn add_rule(&mut self, rule: Rule) {
        self.rules.push(rule);
    }

    /// Run bottom-up fixpoint evaluation.
    ///
    /// Starts from the set of base facts and applies all rules until no new
    /// facts are derived (fixpoint) or `max_iterations` is exceeded.
    ///
    /// Returns the complete set of derived facts (including base facts).
    pub fn evaluate(&self) -> LogicResult<HashSet<Atom>> {
        let mut derived = self.facts.clone();
        let mut iteration = 0;

        loop {
            if iteration >= self.max_iterations {
                self.last_iterations.set(iteration);
                return Err(LogicError::InvalidInput(format!(
                    "DatalogEngine: fixpoint not reached after {} iterations",
                    self.max_iterations
                )));
            }

            let new_facts = self.derive_one_step(&derived);
            let added_any = new_facts.into_iter().fold(false, |acc, fact| {
                let is_new = !derived.contains(&fact);
                if is_new {
                    derived.insert(fact);
                }
                acc || is_new
            });

            iteration += 1;

            if !added_any {
                break;
            }
        }

        self.last_iterations.set(iteration);
        Ok(derived)
    }

    /// Derive all facts producible in one step from the current `known` set.
    fn derive_one_step(&self, known: &HashSet<Atom>) -> Vec<Atom> {
        let mut new_facts = Vec::new();

        for rule in &self.rules {
            // Generate all substitutions that satisfy the body
            let substs = self.join_body(&rule.body, known);
            for subst in substs {
                let head = apply_subst(&rule.head, &subst);
                if head.is_ground() {
                    new_facts.push(head);
                }
            }
        }

        new_facts
    }

    /// Find all substitutions that satisfy every atom in `body` against `known` facts.
    ///
    /// Uses a recursive nested-loop join (sound for Datalog's stratified semantics).
    fn join_body(&self, body: &[Atom], known: &HashSet<Atom>) -> Vec<Substitution> {
        if body.is_empty() {
            return vec![HashMap::new()];
        }

        let first = &body[0];
        let rest = &body[1..];

        let mut results = Vec::new();

        // For each known ground fact, try to unify with `first`
        for known_atom in known {
            let empty: Substitution = HashMap::new();
            if let Some(subst) = unify(first, known_atom, &empty) {
                // Recurse on remaining body with the extended substitution
                let rest_atoms: Vec<Atom> = rest.iter().map(|a| apply_subst(a, &subst)).collect();
                for mut child_subst in self.join_body(&rest_atoms, known) {
                    // Merge subst into child_subst
                    for (k, v) in &subst {
                        child_subst.entry(k.clone()).or_insert_with(|| v.clone());
                    }
                    results.push(child_subst);
                }
            }
        }

        results
    }

    /// Find all substitutions for which `apply_subst(query, subst)` is in `derived`.
    pub fn query(&self, derived: &HashSet<Atom>, query: &Atom) -> Vec<Substitution> {
        let mut results = Vec::new();

        for fact in derived {
            let empty: Substitution = HashMap::new();
            if let Some(subst) = unify(query, fact, &empty) {
                results.push(subst);
            }
        }

        results
    }

    /// Check whether a specific ground atom is in the derived set.
    pub fn entails(&self, derived: &HashSet<Atom>, atom: &Atom) -> bool {
        derived.contains(atom)
    }

    /// Return the number of fixpoint iterations taken during the last call to `evaluate`.
    pub fn last_iterations(&self) -> usize {
        self.last_iterations.get()
    }
}

// ============================================================================
// SignalFactBridge
// ============================================================================

/// A bridge that translates signal constraint checks into Datalog facts,
/// enabling logic-based violation reasoning over streaming data.
///
/// For each signal dimension and time step, one of two atoms is asserted:
/// - `in_range(dim_N, step_M, ok)` — value is within bounds
/// - `in_range(dim_N, step_M, violation)` — value is out of bounds
pub struct SignalFactBridge {
    engine: DatalogEngine,
    /// Current time step counter
    pub step: usize,
}

impl Default for SignalFactBridge {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalFactBridge {
    /// Create a new bridge at step 0.
    pub fn new() -> Self {
        Self {
            engine: DatalogEngine::new(),
            step: 0,
        }
    }

    /// Assert a bounds fact for `dim` at the current step.
    ///
    /// Adds `in_range(dim_N, step_M, ok)` if `lo <= value <= hi`,
    /// otherwise `in_range(dim_N, step_M, violation)`.
    pub fn assert_bounds_fact(&mut self, dim: usize, value: f32, lo: f32, hi: f32) {
        let status = if value >= lo && value <= hi {
            "ok"
        } else {
            "violation"
        };

        let atom = Atom {
            predicate: "in_range".to_string(),
            args: vec![
                Term::sym(&format!("dim_{dim}")),
                Term::sym(&format!("step_{}", self.step)),
                Term::sym(status),
            ],
        };

        self.engine.add_fact(atom);
    }

    /// Assert an arbitrary custom fact into the engine.
    pub fn assert_custom_fact(&mut self, predicate: &str, args: Vec<Term>) {
        self.engine.add_fact(Atom {
            predicate: predicate.to_string(),
            args,
        });
    }

    /// Add a derivation rule to the underlying engine.
    pub fn add_constraint_rule(&mut self, rule: Rule) {
        self.engine.add_rule(rule);
    }

    /// Derive all facts and check whether any `violation` atom is entailed.
    ///
    /// A `violation` is signalled by the presence of any `in_range(_, _, violation)` fact,
    /// or any atom whose predicate is "violation".
    pub fn has_violations(&self) -> LogicResult<bool> {
        let derived = self.engine.evaluate()?;
        let has = derived.iter().any(|atom| {
            // Check for in_range(..., violation) facts
            if atom.predicate == "in_range" {
                if let Some(Term::Sym(status)) = atom.args.last() {
                    return status == "violation";
                }
            }
            // Check for standalone "violation" predicate
            atom.predicate == "violation"
        });
        Ok(has)
    }

    /// Return the list of dimension indices that have violations at any step.
    pub fn violated_dims(&self) -> LogicResult<Vec<usize>> {
        let derived = self.engine.evaluate()?;
        let mut dims = Vec::new();

        for atom in &derived {
            if atom.predicate == "in_range" && atom.args.len() == 3 {
                if let Term::Sym(status) = &atom.args[2] {
                    if status == "violation" {
                        if let Term::Sym(dim_str) = &atom.args[0] {
                            if let Some(rest) = dim_str.strip_prefix("dim_") {
                                if let Ok(dim) = rest.parse::<usize>() {
                                    if !dims.contains(&dim) {
                                        dims.push(dim);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        dims.sort();
        Ok(dims)
    }

    /// Advance to the next time step.
    pub fn advance_step(&mut self) {
        self.step += 1;
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    fn atom(pred: &str, args: Vec<Term>) -> Atom {
        Atom {
            predicate: pred.to_string(),
            args,
        }
    }

    #[test]
    fn test_fact_entailment() {
        let mut engine = DatalogEngine::new();
        let fact = atom("foo", vec![Term::sym("a")]);
        engine.add_fact(fact.clone());
        let derived = engine.evaluate().expect("evaluate failed");
        assert!(
            engine.entails(&derived, &fact),
            "directly added fact should be entailed"
        );
    }

    #[test]
    fn test_rule_derivation_single_step() {
        let mut engine = DatalogEngine::new();

        // parent(tom, bob)
        engine.add_fact(atom("parent", vec![Term::sym("tom"), Term::sym("bob")]));

        // ancestor(X, Y) :- parent(X, Y)
        engine.add_rule(Rule::new(
            atom("ancestor", vec![Term::var("X"), Term::var("Y")]),
            vec![atom("parent", vec![Term::var("X"), Term::var("Y")])],
        ));

        let derived = engine.evaluate().expect("evaluate failed");

        let expected = atom("ancestor", vec![Term::sym("tom"), Term::sym("bob")]);
        assert!(
            engine.entails(&derived, &expected),
            "ancestor(tom, bob) should be derived from parent(tom, bob)"
        );
    }

    #[test]
    fn test_rule_derivation_chained() {
        let mut engine = DatalogEngine::new();

        // parent(a, b), parent(b, c)
        engine.add_fact(atom("parent", vec![Term::sym("a"), Term::sym("b")]));
        engine.add_fact(atom("parent", vec![Term::sym("b"), Term::sym("c")]));

        // ancestor(X, Y) :- parent(X, Y)
        engine.add_rule(Rule::new(
            atom("ancestor", vec![Term::var("X"), Term::var("Y")]),
            vec![atom("parent", vec![Term::var("X"), Term::var("Y")])],
        ));

        // ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)
        engine.add_rule(Rule::new(
            atom("ancestor", vec![Term::var("X"), Term::var("Z")]),
            vec![
                atom("parent", vec![Term::var("X"), Term::var("Y")]),
                atom("ancestor", vec![Term::var("Y"), Term::var("Z")]),
            ],
        ));

        let derived = engine.evaluate().expect("evaluate failed");

        let expected = atom("ancestor", vec![Term::sym("a"), Term::sym("c")]);
        assert!(
            engine.entails(&derived, &expected),
            "ancestor(a, c) should be derivable via 2-hop chain"
        );
    }

    #[test]
    fn test_unification_vars() {
        // unify Atom("p", [Var("X"), Sym("b")]) with Atom("p", [Sym("a"), Sym("b")])
        let a = atom("p", vec![Term::var("X"), Term::sym("b")]);
        let b = atom("p", vec![Term::sym("a"), Term::sym("b")]);
        let empty: Substitution = HashMap::new();
        let result = unify(&a, &b, &empty);

        assert!(result.is_some(), "unification should succeed");
        let subst = result.unwrap();
        assert_eq!(
            subst.get("X"),
            Some(&Term::sym("a")),
            "X should be bound to Sym(\"a\")"
        );
    }

    #[test]
    fn test_query_returns_substitutions() {
        let mut engine = DatalogEngine::new();

        // parent(alice, bob), parent(alice, carol)
        engine.add_fact(atom("parent", vec![Term::sym("alice"), Term::sym("bob")]));
        engine.add_fact(atom("parent", vec![Term::sym("alice"), Term::sym("carol")]));

        let derived = engine.evaluate().expect("evaluate failed");

        // Query: parent(alice, Y)
        let query = atom("parent", vec![Term::sym("alice"), Term::var("Y")]);
        let substs = engine.query(&derived, &query);

        assert_eq!(
            substs.len(),
            2,
            "should find 2 substitutions for parent(alice, Y)"
        );
    }

    #[test]
    fn test_fixpoint_terminates() {
        // Cyclic rule: a(X) :- a(X)  — should terminate (no new facts added)
        let mut engine = DatalogEngine::with_max_iterations(10);
        engine.add_fact(atom("a", vec![Term::sym("x")]));
        engine.add_rule(Rule::new(
            atom("a", vec![Term::var("X")]),
            vec![atom("a", vec![Term::var("X")])],
        ));

        let result = engine.evaluate();
        // Should succeed (no new facts derived, fixpoint reached immediately)
        assert!(result.is_ok(), "cyclic rule should terminate: {:?}", result);
    }

    #[test]
    fn test_bridge_in_range() {
        let mut bridge = SignalFactBridge::new();
        bridge.assert_bounds_fact(0, 0.5, 0.0, 1.0);
        let has_viol = bridge.has_violations().expect("has_violations failed");
        assert!(!has_viol, "0.5 in [0, 1] should have no violations");
    }

    #[test]
    fn test_bridge_out_of_range() {
        let mut bridge = SignalFactBridge::new();
        bridge.assert_bounds_fact(0, 2.0, 0.0, 1.0);
        let has_viol = bridge.has_violations().expect("has_violations failed");
        assert!(has_viol, "2.0 not in [0, 1] should have a violation");
    }

    #[test]
    fn test_bridge_advance_step() {
        let mut bridge = SignalFactBridge::new();
        assert_eq!(bridge.step, 0, "step should start at 0");
        bridge.advance_step();
        assert_eq!(bridge.step, 1, "step should be 1 after advance_step()");
    }
}