nibli-reason 0.1.0

Reasoning engine — backward-chaining inference over typed fact store
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
use super::*;

// ─── C5: Description term opacity tests ──────────────────────

/// Helper: make an assertion with a Description term in x1.
fn make_desc_assertion(desc_name: &str, predicate: &str) -> LogicBuffer {
    let mut nodes = Vec::new();
    let root = pred(
        &mut nodes,
        predicate,
        vec![
            LogicalTerm::Description(desc_name.to_string()),
            LogicalTerm::Unspecified,
        ],
    );
    LogicBuffer {
        nodes,
        roots: vec![root],
    }
}

/// Helper: make a query with a Description term in x1.
fn make_desc_query(desc_name: &str, predicate: &str) -> LogicBuffer {
    make_desc_assertion(desc_name, predicate)
}

#[test]
fn test_desc_gets_indomain() {
    // Assert a fact with Description term → Desc should be in InDomain
    let kb = new_kb();
    assert_buf(&kb, make_desc_assertion("gerku", "sutra"));
    let inner = kb.inner.borrow();
    assert!(
        inner.known_descriptions.contains("gerku"),
        "expected 'gerku' in known_descriptions"
    );
}

#[test]
fn test_desc_conjunction_introduction() {
    // Two facts about same Desc term → conjunction should be derivable
    let kb = new_kb();
    assert_buf(&kb, make_desc_assertion("gerku", "danlu"));
    assert_buf(&kb, make_desc_assertion("gerku", "sutra"));

    // Query And(danlu(Desc "gerku"), sutra(Desc "gerku"))
    let mut nodes = Vec::new();
    let p1 = pred(
        &mut nodes,
        "danlu",
        vec![
            LogicalTerm::Description("gerku".to_string()),
            LogicalTerm::Unspecified,
        ],
    );
    let p2 = pred(
        &mut nodes,
        "sutra",
        vec![
            LogicalTerm::Description("gerku".to_string()),
            LogicalTerm::Unspecified,
        ],
    );
    let root = and(&mut nodes, p1, p2);
    assert!(
        query(
            &kb,
            LogicBuffer {
                nodes,
                roots: vec![root]
            }
        ),
        "conjunction of two Desc-term facts should hold via conjunction introduction"
    );
}

#[test]
fn test_desc_does_not_trigger_rule_without_restrictor() {
    // Assert sutra(Desc "gerku") (but NOT gerku(Desc "gerku"))
    // Assert rule: ro lo gerku cu danlu (∀x. gerku(x) → danlu(x))
    // Query danlu(Desc "gerku") → should FAIL (the restrictor isn't satisfied)
    let kb = new_kb();
    assert_buf(&kb, make_desc_assertion("gerku", "sutra"));
    assert_buf(&kb, make_universal("gerku", "danlu"));
    assert!(
        query_false(&kb, make_desc_query("gerku", "danlu")),
        "universal rule should NOT fire without restrictor being satisfied for Desc term"
    );
}

#[test]
fn test_desc_triggers_rule_when_restrictor_satisfied() {
    // Assert gerku(Desc "gerku") AND sutra(Desc "gerku")
    // Assert rule: ro lo gerku cu danlu
    // Query danlu(Desc "gerku") → should SUCCEED
    let kb = new_kb();
    assert_buf(&kb, make_desc_assertion("gerku", "gerku"));
    assert_buf(&kb, make_desc_assertion("gerku", "sutra"));
    assert_buf(&kb, make_universal("gerku", "danlu"));
    assert!(
        query(&kb, make_desc_query("gerku", "danlu")),
        "universal rule should fire when restrictor IS satisfied for Desc term"
    );
}

#[test]
fn test_desc_exists_witness() {
    // Assert sutra(Desc "gerku") → ∃x. sutra(x) should find Desc "gerku" as witness
    let kb = new_kb();
    assert_buf(&kb, make_desc_assertion("gerku", "sutra"));

    let mut nodes = Vec::new();
    let body = pred(
        &mut nodes,
        "sutra",
        vec![
            LogicalTerm::Variable("x".to_string()),
            LogicalTerm::Unspecified,
        ],
    );
    let root = exists(&mut nodes, "x", body);
    assert!(
        query(
            &kb,
            LogicBuffer {
                nodes,
                roots: vec![root]
            }
        ),
        "existential query should find Desc term as witness"
    );
}

#[test]
fn test_backward_chain_derives_facts() {
    // Assert a fact and a rule, then verify backward-chaining derives conclusions
    let kb = new_kb();
    // Assert: gerku(alis)
    assert_buf(&kb, make_assertion("alis", "gerku"));

    // Assert: ∀x. ¬gerku(x) ∨ danlu(x) (i.e., gerku → danlu)
    assert_buf(&kb, make_universal("gerku", "danlu"));

    // Query: danlu(alis) — should be derived via backward-chaining
    assert!(
        query(&kb, make_query("alis", "danlu")),
        "backward-chaining should derive danlu(alis) from gerku(alis) and universal rule"
    );
}

// ─── Event-decomposed universal rule tests ──────────────────────

#[test]
fn test_event_decomposed_rule_fires() {
    let kb = new_kb();
    assert_buf(&kb, make_event_assertion("alis", "gerku"));
    assert_buf(&kb, make_event_universal("gerku", "danlu"));
    assert!(
        query(&kb, make_event_query("alis", "danlu")),
        "event-decomposed rule should derive danlu(alis) from gerku(alis)"
    );
}

/// Build a NEGATED event-decomposed universal rule (`ro lo X poi na P cu Q`):
///   ForAll(_v0, Or(
///     Not(Not(Exists(_ev0, And(P(_ev0), P_x1(_ev0, _v0))))),  // poi na P restrictor
///     Exists(_ev1, And(Q(_ev1), Q_x1(_ev1, _v0)))             // consequent
///   ))
/// `decompose_implication` strips the OUTER `Not`, leaving `Not(Exists(...))` as the
/// CONDITION — the negated existential group this fix compiles into a NAF check.
fn make_negated_event_universal(restrictor: &str, consequent: &str) -> LogicBuffer {
    let mut nodes = Vec::new();
    let p_type = pred(
        &mut nodes,
        restrictor,
        vec![LogicalTerm::Variable("_ev0".to_string())],
    );
    let p_role = pred(
        &mut nodes,
        &format!("{}_x1", restrictor),
        vec![
            LogicalTerm::Variable("_ev0".to_string()),
            LogicalTerm::Variable("_v0".to_string()),
        ],
    );
    let p_and = and(&mut nodes, p_type, p_role);
    let p_exists = exists(&mut nodes, "_ev0", p_and);
    let neg_restrictor = not(&mut nodes, p_exists); // Not(Exists(...)) — "x does NOT P"

    let q_type = pred(
        &mut nodes,
        consequent,
        vec![LogicalTerm::Variable("_ev1".to_string())],
    );
    let q_role = pred(
        &mut nodes,
        &format!("{}_x1", consequent),
        vec![
            LogicalTerm::Variable("_ev1".to_string()),
            LogicalTerm::Variable("_v0".to_string()),
        ],
    );
    let q_and = and(&mut nodes, q_type, q_role);
    let q_exists = exists(&mut nodes, "_ev1", q_and);

    let neg = not(&mut nodes, neg_restrictor); // Not(Not(Exists(...)))
    let disj = or(&mut nodes, neg, q_exists);
    let root = forall(&mut nodes, "_v0", disj);
    LogicBuffer {
        nodes,
        roots: vec![root],
    }
}

#[test]
fn test_negated_event_decomposed_rule_compiles_and_fires() {
    let kb = new_kb();
    // "every X that does NOT zanru (consent) is bilga (obligated)".
    assert_buf(&kb, make_negated_event_universal("zanru", "bilga"));
    // alis is a known entity (a person) but has NOT consented.
    assert_buf(&kb, make_event_assertion("alis", "prenu"));
    assert!(
        query(&kb, make_event_query("alis", "bilga")),
        "no consent witness → ¬∃ holds (NAF) → the rule fires: bilga(alis) TRUE"
    );
    // Now alis consents → ¬∃ is false → the rule must not fire.
    assert_buf(&kb, make_event_assertion("alis", "zanru"));
    assert!(
        matches!(
            query_result(&kb, make_event_query("alis", "bilga")),
            QueryResult::False
        ),
        "consent asserted → ¬∃ false → rule does not fire: bilga(alis) FALSE"
    );
}

#[test]
fn test_negated_event_group_rule_shape() {
    let kb = new_kb();
    assert_buf(&kb, make_negated_event_universal("zanru", "bilga"));
    let inner = kb.inner.borrow();
    let rule = inner
        .universal_rules
        .values()
        .flatten()
        .find(|r| r.typed_conclusions.iter().any(|c| c.relation() == "bilga"))
        .expect("the bilga rule should be registered");
    assert_eq!(
        rule.negated_exists_groups.len(),
        1,
        "one negated-exists group"
    );
    let rels: Vec<&str> = rule.negated_exists_groups[0]
        .conditions
        .iter()
        .map(|c| c.relation())
        .collect();
    assert_eq!(rels, vec!["zanru", "zanru_x1"], "group inner conjuncts");
    assert!(
        rule.typed_conditions.is_empty(),
        "the negated group is NOT a flat typed condition"
    );
}

#[test]
fn test_negated_event_group_self_reference_rejected() {
    let kb = new_kb();
    // "every X that does NOT danlu is danlu" — the negated group anchors the rule's
    // own conclusion → negative self-loop → unstratifiable → rejected.
    let r = kb.assert_fact_inner(
        make_negated_event_universal("danlu", "danlu"),
        String::new(),
    );
    assert!(
        r.is_err(),
        "a negated group anchoring its own conclusion must be rejected as unstratifiable"
    );
}

/// Build `Exists(ev, And(P(ev), P_x1(ev, entity)))` for a GROUND entity constant.
fn event_operand(nodes: &mut Vec<LogicNode>, ev: &str, predicate: &str, entity: &str) -> u32 {
    let p_type = pred(
        nodes,
        predicate,
        vec![LogicalTerm::Variable(ev.to_string())],
    );
    let p_role = pred(
        nodes,
        &format!("{}_x1", predicate),
        vec![
            LogicalTerm::Variable(ev.to_string()),
            LogicalTerm::Constant(entity.to_string()),
        ],
    );
    let p_and = and(nodes, p_type, p_role);
    exists(nodes, ev, p_and)
}

/// Ground event-decomposed material conditional: Or(Not(A), B), operands over a constant.
fn make_event_material_conditional(
    entity: &str,
    antecedent: &str,
    consequent: &str,
) -> LogicBuffer {
    let mut nodes = Vec::new();
    let a = event_operand(&mut nodes, "_ev0", antecedent, entity);
    let b = event_operand(&mut nodes, "_ev1", consequent, entity);
    let neg_a = not(&mut nodes, a);
    let root = or(&mut nodes, neg_a, b);
    LogicBuffer {
        nodes,
        roots: vec![root],
    }
}

/// Ground event-decomposed biconditional: And(Or(Not(A),B), Or(Not(B),A)).
fn make_event_biconditional(entity: &str, a: &str, b: &str) -> LogicBuffer {
    let mut nodes = Vec::new();
    let a1 = event_operand(&mut nodes, "_ev0", a, entity);
    let b1 = event_operand(&mut nodes, "_ev1", b, entity);
    let neg_a1 = not(&mut nodes, a1);
    let impl1 = or(&mut nodes, neg_a1, b1);
    let b2 = event_operand(&mut nodes, "_ev2", b, entity);
    let a2 = event_operand(&mut nodes, "_ev3", a, entity);
    let neg_b2 = not(&mut nodes, b2);
    let impl2 = or(&mut nodes, neg_b2, a2);
    let root = and(&mut nodes, impl1, impl2);
    LogicBuffer {
        nodes,
        roots: vec![root],
    }
}

#[test]
fn test_event_decomposed_ground_material_conditional() {
    // Or(Not(∃e. gerku(e) ∧ gerku_x1(e, adam)), ∃e2. danlu(e2) ∧ danlu_x1(e2, adam))
    // + gerku(adam) ⇒ danlu(adam) (modus ponens over event-decomposed operands).
    let kb = new_kb();
    assert_buf(
        &kb,
        make_event_material_conditional("adam", "gerku", "danlu"),
    );
    assert_buf(&kb, make_event_assertion("adam", "gerku"));
    assert!(
        query(&kb, make_event_query("adam", "danlu")),
        "ground material conditional should derive danlu(adam) from gerku(adam)"
    );
}

#[test]
fn test_event_decomposed_ground_material_conditional_no_antecedent() {
    // Negative control: without the antecedent fact the conclusion is not derivable.
    let kb = new_kb();
    assert_buf(
        &kb,
        make_event_material_conditional("adam", "gerku", "danlu"),
    );
    assert!(
        query_false(&kb, make_event_query("adam", "danlu")),
        "danlu(adam) must NOT hold without gerku(adam)"
    );
}

#[test]
fn test_event_decomposed_ground_biconditional_both_directions() {
    // A ↔ B reasons both ways over event-decomposed operands (no CycleCut).
    let kb = new_kb();
    assert_buf(&kb, make_event_biconditional("adam", "gerku", "danlu"));
    assert_buf(&kb, make_event_assertion("adam", "gerku"));
    assert!(
        query(&kb, make_event_query("adam", "danlu")),
        "biconditional: danlu(adam) should derive from gerku(adam) (forward)"
    );

    let kb2 = new_kb();
    assert_buf(&kb2, make_event_biconditional("adam", "gerku", "danlu"));
    assert_buf(&kb2, make_event_assertion("adam", "danlu"));
    assert!(
        query(&kb2, make_event_query("adam", "gerku")),
        "biconditional: gerku(adam) should derive from danlu(adam) (reverse)"
    );
}

#[test]
fn test_event_decomposed_rule_selective() {
    let kb = new_kb();
    assert_buf(&kb, make_event_assertion("alis", "gerku"));
    assert_buf(&kb, make_event_assertion("bob", "mlatu"));
    assert_buf(&kb, make_event_universal("gerku", "danlu"));
    assert!(
        query(&kb, make_event_query("alis", "danlu")),
        "danlu(alis) should hold (alis is a gerku)"
    );
    assert!(
        query_false(&kb, make_event_query("bob", "danlu")),
        "danlu(bob) should NOT hold (bob is a mlatu, not gerku)"
    );
}

#[test]
fn test_event_decomposed_entity_after_rule() {
    let kb = new_kb();
    // Add rule first, then fact — should still fire after saturation
    assert_buf(&kb, make_event_universal("gerku", "danlu"));
    assert_buf(&kb, make_event_assertion("alis", "gerku"));
    assert!(
        query(&kb, make_event_query("alis", "danlu")),
        "rule should fire even when added before fact"
    );
}

#[test]
fn test_event_decomposed_temporal_rule() {
    let kb = new_kb();
    // Assert: Past(Exists(_ev0, And(gerku(_ev0), gerku_x1(_ev0, alis))))
    let mut a_nodes = Vec::new();
    let p_type = pred(
        &mut a_nodes,
        "gerku",
        vec![LogicalTerm::Variable("_ev0".to_string())],
    );
    let p_role = pred(
        &mut a_nodes,
        "gerku_x1",
        vec![
            LogicalTerm::Variable("_ev0".to_string()),
            LogicalTerm::Constant("alis".to_string()),
        ],
    );
    let p_and = and(&mut a_nodes, p_type, p_role);
    let p_exists = exists(&mut a_nodes, "_ev0", p_and);
    let a_root = past(&mut a_nodes, p_exists);
    assert_buf(
        &kb,
        LogicBuffer {
            nodes: a_nodes,
            roots: vec![a_root],
        },
    );

    // Add timeless rule: ro lo gerku ku danlu (event-decomposed)
    assert_buf(&kb, make_event_universal("gerku", "danlu"));

    // Query: Past(Exists(_ev0, And(danlu(_ev0), danlu_x1(_ev0, alis))))
    let mut q_nodes = Vec::new();
    let q_type = pred(
        &mut q_nodes,
        "danlu",
        vec![LogicalTerm::Variable("_ev0".to_string())],
    );
    let q_role = pred(
        &mut q_nodes,
        "danlu_x1",
        vec![
            LogicalTerm::Variable("_ev0".to_string()),
            LogicalTerm::Constant("alis".to_string()),
        ],
    );
    let q_and = and(&mut q_nodes, q_type, q_role);
    let q_exists = exists(&mut q_nodes, "_ev0", q_and);
    let q_root = past(&mut q_nodes, q_exists);
    assert!(
        query(
            &kb,
            LogicBuffer {
                nodes: q_nodes,
                roots: vec![q_root],
            }
        ),
        "temporal lifting should derive Past(danlu(alis)) from Past(gerku(alis)) and timeless rule"
    );
}

#[test]
fn test_event_decomposed_multi_hop() {
    let kb = new_kb();
    assert_buf(&kb, make_event_assertion("alis", "gerku"));
    assert_buf(&kb, make_event_universal("gerku", "danlu"));
    assert_buf(&kb, make_event_universal("danlu", "xanlu"));
    assert!(
        query(&kb, make_event_query("alis", "xanlu")),
        "multi-hop: gerku→danlu→xanlu should derive xanlu(alis)"
    );
}

#[test]
fn test_event_decomposed_proof_trace() {
    let kb = new_kb();
    assert_buf(&kb, make_event_assertion("alis", "gerku"));
    assert_buf(&kb, make_event_universal("gerku", "danlu"));

    // Query with proof trace
    let (holds, trace) = kb
        .query_entailment_with_proof_inner(make_event_query("alis", "danlu"))
        .unwrap();
    assert!(
        holds.is_true(),
        "entailment should hold for derived event-decomposed fact"
    );

    // Check that the proof trace contains a Derived step
    let has_derived = trace
        .steps
        .iter()
        .any(|step| matches!(&step.rule, ProofRule::Derived { .. }));
    assert!(
        has_derived,
        "proof trace should contain at least one Derived step for rule-derived fact"
    );
}

#[test]
fn test_event_decomposed_existential_import() {
    let kb = new_kb();
    // Only add the rule (no ground facts) — existential-import presupposition should
    // create Skolem constants that make the restrictor domain non-empty
    assert_buf(&kb, make_event_universal("gerku", "danlu"));

    // The existential-import presupposition should have created event + entity Skolems
    // such that gerku(sk_ev) and gerku_x1(sk_ev, sk_entity) hold.
    // Query: exists something that is a gerku (via existential-import presupposition)
    let mut q_nodes = Vec::new();
    let q_type = pred(
        &mut q_nodes,
        "gerku",
        vec![LogicalTerm::Variable("_ev0".to_string())],
    );
    let q_role = pred(
        &mut q_nodes,
        "gerku_x1",
        vec![
            LogicalTerm::Variable("_ev0".to_string()),
            LogicalTerm::Variable("_v0".to_string()),
        ],
    );
    let q_and = and(&mut q_nodes, q_type, q_role);
    let q_ev = exists(&mut q_nodes, "_ev0", q_and);
    let q_root = exists(&mut q_nodes, "_v0", q_ev);
    assert!(
        query(
            &kb,
            LogicBuffer {
                nodes: q_nodes,
                roots: vec![q_root],
            }
        ),
        "xorlo presupposition should make ∃x.∃e. gerku(e)∧gerku_x1(e,x) hold"
    );
}