owl-dl-core 0.3.0

Core IR, normalization, and shared utilities for the rustdl OWL DL reasoner
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
//! Normalization passes that prepare an ontology for reasoning.
//!
//! Phase 1 of the strategy. This module provides:
//!
//! - [`to_nnf`] — Negation Normal Form, pushing `Not` to atomic positions.
//! - [`nnf_axioms`] — apply NNF to every concept embedded in every axiom of
//!   an ontology, returning a fresh `Vec<Axiom>` with the original axiom
//!   list untouched.
//!
//! Coming next: absorption (binary, role, nominal) and the structural
//! (Tseitin-style) transformation.

use crate::ConceptPool;
use crate::ir::{ConceptExpr, ConceptId};
use crate::ontology::{Axiom, InternalOntology};

/// Rewrite a concept into Negation Normal Form: every `Not` is pushed down
/// until it wraps an atomic concept (`Atomic`, `Nominal`, or
/// `SelfRestriction`). Pure transformation — the input pool is mutated only
/// by interning the new sub-expressions.
///
/// The standard SROIQ NNF rules:
///
/// | input            | NNF                       |
/// |------------------|---------------------------|
/// | `¬⊤`             | `⊥`                       |
/// | `¬⊥`             | `⊤`                       |
/// | `¬¬C`            | `nnf(C)`                  |
/// | `¬(C ⊓ D)`       | `nnf(¬C) ⊔ nnf(¬D)`       |
/// | `¬(C ⊔ D)`       | `nnf(¬C) ⊓ nnf(¬D)`       |
/// | `¬∃R.C`          | `∀R. nnf(¬C)`             |
/// | `¬∀R.C`          | `∃R. nnf(¬C)`             |
/// | `¬(≥0 R.C)`      | `⊥` (≥0 is satisfied by everything) |
/// | `¬(≥n R.C)`, n≥1 | `≤(n-1) R. nnf(C)`        |
/// | `¬(≤n R.C)`      | `≥(n+1) R. nnf(C)`        |
///
/// In the cardinality cases the inner `C` is *not* negated: the restriction
/// itself is what's flipped. `C` is re-normalized so any deeper negations
/// surface to leaves.
#[must_use]
pub fn to_nnf(cid: ConceptId, pool: &mut ConceptPool) -> ConceptId {
    let expr = pool.get(cid).clone();
    match expr {
        ConceptExpr::Top
        | ConceptExpr::Bot
        | ConceptExpr::Atomic(_)
        | ConceptExpr::Nominal(_)
        | ConceptExpr::SelfRestriction(_) => cid,
        ConceptExpr::Not(inner) => push_negation_in(inner, pool),
        ConceptExpr::And(args) => {
            let normalized: Vec<ConceptId> = args.iter().map(|&c| to_nnf(c, pool)).collect();
            pool.and(normalized)
        }
        ConceptExpr::Or(args) => {
            let normalized: Vec<ConceptId> = args.iter().map(|&c| to_nnf(c, pool)).collect();
            pool.or(normalized)
        }
        ConceptExpr::Some(role, c) => {
            let c_nnf = to_nnf(c, pool);
            pool.some(role, c_nnf)
        }
        ConceptExpr::All(role, c) => {
            let c_nnf = to_nnf(c, pool);
            pool.all(role, c_nnf)
        }
        ConceptExpr::Min(n, role, c) => {
            let c_nnf = to_nnf(c, pool);
            pool.min(n, role, c_nnf)
        }
        ConceptExpr::Max(n, role, c) => {
            let c_nnf = to_nnf(c, pool);
            pool.max(n, role, c_nnf)
        }
    }
}

/// Compute `nnf(¬C)` given `C` (already assumed NNF or convertible).
/// Exposed for the tableau's choose rule, which needs the NNF
/// complement of a concept when branching on `≤n R.C`.
#[must_use]
pub fn nnf_complement(cid: ConceptId, pool: &mut ConceptPool) -> ConceptId {
    push_negation_in(cid, pool)
}

/// Helper: compute `nnf(¬C)` given the `C` (not its negation).
fn push_negation_in(cid: ConceptId, pool: &mut ConceptPool) -> ConceptId {
    let expr = pool.get(cid).clone();
    match expr {
        ConceptExpr::Top => pool.bot(),
        ConceptExpr::Bot => pool.top(),
        ConceptExpr::Atomic(_) | ConceptExpr::Nominal(_) | ConceptExpr::SelfRestriction(_) => {
            pool.not(cid)
        }
        // ¬¬C = nnf(C)
        ConceptExpr::Not(inner) => to_nnf(inner, pool),
        // ¬(C ⊓ D) = nnf(¬C) ⊔ nnf(¬D)
        ConceptExpr::And(args) => {
            let negated: Vec<ConceptId> = args.iter().map(|&c| push_negation_in(c, pool)).collect();
            pool.or(negated)
        }
        // ¬(C ⊔ D) = nnf(¬C) ⊓ nnf(¬D)
        ConceptExpr::Or(args) => {
            let negated: Vec<ConceptId> = args.iter().map(|&c| push_negation_in(c, pool)).collect();
            pool.and(negated)
        }
        // ¬∃R.C = ∀R. nnf(¬C)
        ConceptExpr::Some(role, c) => {
            let c_neg = push_negation_in(c, pool);
            pool.all(role, c_neg)
        }
        // ¬∀R.C = ∃R. nnf(¬C)
        ConceptExpr::All(role, c) => {
            let c_neg = push_negation_in(c, pool);
            pool.some(role, c_neg)
        }
        // ¬(≥0 R.C) = ⊥  (any individual has at least 0 R-successors)
        // ¬(≥n R.C) = ≤(n-1) R. nnf(C),  for n ≥ 1
        ConceptExpr::Min(n, role, c) => {
            if n == 0 {
                pool.bot()
            } else {
                let c_nnf = to_nnf(c, pool);
                pool.max(n - 1, role, c_nnf)
            }
        }
        // ¬(≤n R.C) = ≥(n+1) R. nnf(C)
        ConceptExpr::Max(n, role, c) => {
            let c_nnf = to_nnf(c, pool);
            pool.min(n + 1, role, c_nnf)
        }
    }
}

/// Check the NNF invariant: in a properly normalized concept tree, every
/// `Not` directly wraps an `Atomic`, `Nominal`, or `SelfRestriction`.
#[must_use]
pub fn is_nnf(cid: ConceptId, pool: &ConceptPool) -> bool {
    match pool.get(cid) {
        ConceptExpr::Top
        | ConceptExpr::Bot
        | ConceptExpr::Atomic(_)
        | ConceptExpr::Nominal(_)
        | ConceptExpr::SelfRestriction(_) => true,
        ConceptExpr::Not(inner) => matches!(
            pool.get(*inner),
            ConceptExpr::Atomic(_) | ConceptExpr::Nominal(_) | ConceptExpr::SelfRestriction(_)
        ),
        ConceptExpr::And(args) | ConceptExpr::Or(args) => args.iter().all(|&c| is_nnf(c, pool)),
        ConceptExpr::Some(_, c)
        | ConceptExpr::All(_, c)
        | ConceptExpr::Min(_, _, c)
        | ConceptExpr::Max(_, _, c) => is_nnf(*c, pool),
    }
}

/// Apply NNF to every concept embedded in the ontology's axioms, producing
/// a fresh `Vec<Axiom>`. The original `ontology.axioms` is left untouched;
/// only `ontology.concepts` may grow with newly interned sub-expressions.
///
/// The pipeline pattern matches told/definitions: source ontology in,
/// derived view out. Absorption and structural transformation downstream
/// consume the result.
pub fn nnf_axioms(ontology: &mut InternalOntology) -> Vec<Axiom> {
    // Disjoint field access: read `axioms`, mutate `concepts`.
    let axioms = &ontology.axioms;
    let pool = &mut ontology.concepts;
    let mut out = Vec::with_capacity(axioms.len());
    for ax in axioms {
        out.push(nnf_axiom(ax, pool));
    }
    out
}

/// Apply NNF to every `ConceptId` field of a single [`Axiom`]. Axioms with
/// no concept fields (role characteristics, `ABox` assertions, declarations,
/// etc.) are returned unchanged.
fn nnf_axiom(ax: &Axiom, pool: &mut ConceptPool) -> Axiom {
    match ax {
        Axiom::SubClassOf { sub, sup } => Axiom::SubClassOf {
            sub: to_nnf(*sub, pool),
            sup: to_nnf(*sup, pool),
        },
        Axiom::EquivalentClasses(ids) => {
            Axiom::EquivalentClasses(ids.iter().map(|&c| to_nnf(c, pool)).collect())
        }
        Axiom::DisjointClasses(ids) => {
            Axiom::DisjointClasses(ids.iter().map(|&c| to_nnf(c, pool)).collect())
        }
        Axiom::DisjointUnion { class, members } => Axiom::DisjointUnion {
            class: *class,
            members: members.iter().map(|&c| to_nnf(c, pool)).collect(),
        },
        Axiom::ObjectPropertyDomain { role, domain } => Axiom::ObjectPropertyDomain {
            role: *role,
            domain: to_nnf(*domain, pool),
        },
        Axiom::ObjectPropertyRange { role, range } => Axiom::ObjectPropertyRange {
            role: *role,
            range: to_nnf(*range, pool),
        },
        Axiom::ClassAssertion { class, individual } => Axiom::ClassAssertion {
            class: to_nnf(*class, pool),
            individual: *individual,
        },
        // All other variants — RBox characteristics, role hierarchies, ABox
        // assertions, declarations — have no embedded ConceptId.
        other => other.clone(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{ClassId, IndividualId, Role, RoleId};

    fn pool() -> ConceptPool {
        ConceptPool::new()
    }

    #[test]
    fn nnf_of_atomic_is_identity() {
        let mut p = pool();
        let a = p.atomic(ClassId::new(0));
        assert_eq!(to_nnf(a, &mut p), a);
    }

    #[test]
    fn nnf_of_not_top_is_bot() {
        let mut p = pool();
        let t = p.top();
        let not_top = p.not(t);
        let nnf = to_nnf(not_top, &mut p);
        assert_eq!(nnf, p.bot());
    }

    #[test]
    fn nnf_of_not_bot_is_top() {
        let mut p = pool();
        let b = p.bot();
        let not_bot = p.not(b);
        let nnf = to_nnf(not_bot, &mut p);
        assert_eq!(nnf, p.top());
    }

    #[test]
    fn nnf_of_double_negation_is_inner() {
        let mut p = pool();
        let a = p.atomic(ClassId::new(0));
        let not_a = p.not(a);
        let not_not_a = p.not(not_a);
        assert_eq!(to_nnf(not_not_a, &mut p), a);
    }

    #[test]
    fn nnf_of_not_atomic_keeps_one_not() {
        let mut p = pool();
        let a = p.atomic(ClassId::new(0));
        let not_a = p.not(a);
        assert_eq!(to_nnf(not_a, &mut p), not_a);
    }

    #[test]
    fn nnf_pushes_through_and_via_de_morgan() {
        // ¬(A ⊓ B) ≡ ¬A ⊔ ¬B
        let mut p = pool();
        let a = p.atomic(ClassId::new(0));
        let b = p.atomic(ClassId::new(1));
        let and_ab = p.and([a, b]);
        let not_and = p.not(and_ab);
        let result = to_nnf(not_and, &mut p);
        let na = p.not(a);
        let nb = p.not(b);
        let expected = p.or([na, nb]);
        assert_eq!(result, expected);
    }

    #[test]
    fn nnf_pushes_through_or_via_de_morgan() {
        // ¬(A ⊔ B) ≡ ¬A ⊓ ¬B
        let mut p = pool();
        let a = p.atomic(ClassId::new(0));
        let b = p.atomic(ClassId::new(1));
        let or_ab = p.or([a, b]);
        let not_or = p.not(or_ab);
        let result = to_nnf(not_or, &mut p);
        let na = p.not(a);
        let nb = p.not(b);
        let expected = p.and([na, nb]);
        assert_eq!(result, expected);
    }

    #[test]
    fn nnf_pushes_through_some_to_all_with_negated_inner() {
        // ¬∃R.A ≡ ∀R. ¬A
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let some_a = p.some(r, a);
        let not_some = p.not(some_a);
        let result = to_nnf(not_some, &mut p);
        let na = p.not(a);
        let expected = p.all(r, na);
        assert_eq!(result, expected);
    }

    #[test]
    fn nnf_pushes_through_all_to_some_with_negated_inner() {
        // ¬∀R.A ≡ ∃R. ¬A
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let all_a = p.all(r, a);
        let not_all = p.not(all_a);
        let result = to_nnf(not_all, &mut p);
        let na = p.not(a);
        let expected = p.some(r, na);
        assert_eq!(result, expected);
    }

    #[test]
    fn nnf_of_not_min_positive_n_becomes_max_n_minus_one() {
        // ¬(≥3 R.A) ≡ ≤2 R.A     (inner A stays positive)
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let min3 = p.min(3, r, a);
        let not_min3 = p.not(min3);
        let result = to_nnf(not_min3, &mut p);
        let expected = p.max(2, r, a);
        assert_eq!(result, expected);
    }

    #[test]
    fn nnf_of_not_min_zero_is_bot() {
        // ¬(≥0 R.A) ≡ ⊥
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let min0 = p.min(0, r, a);
        let not_min0 = p.not(min0);
        let result = to_nnf(not_min0, &mut p);
        assert_eq!(result, p.bot());
    }

    #[test]
    fn nnf_of_not_max_becomes_min_n_plus_one() {
        // ¬(≤2 R.A) ≡ ≥3 R.A
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let max2 = p.max(2, r, a);
        let not_max2 = p.not(max2);
        let result = to_nnf(not_max2, &mut p);
        let expected = p.min(3, r, a);
        assert_eq!(result, expected);
    }

    #[test]
    fn nnf_of_not_nominal_keeps_one_not() {
        // ¬{a} stays as Not(Nominal) — there's no equivalent positive form.
        let mut p = pool();
        let n = p.nominal(IndividualId::new(0));
        let not_n = p.not(n);
        assert_eq!(to_nnf(not_n, &mut p), not_n);
    }

    #[test]
    fn nnf_of_not_self_restriction_keeps_one_not() {
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let s = p.self_restriction(r);
        let not_s = p.not(s);
        assert_eq!(to_nnf(not_s, &mut p), not_s);
    }

    #[test]
    fn nested_negation_through_nested_structure() {
        // ¬(A ⊓ ∃R.B) ≡ ¬A ⊔ ∀R.¬B
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let b = p.atomic(ClassId::new(1));
        let some_b = p.some(r, b);
        let conj = p.and([a, some_b]);
        let neg = p.not(conj);
        let result = to_nnf(neg, &mut p);
        let na = p.not(a);
        let nb = p.not(b);
        let all_nb = p.all(r, nb);
        let expected = p.or([na, all_nb]);
        assert_eq!(result, expected);
    }

    #[test]
    fn is_nnf_recognizes_nnf_forms() {
        let mut p = pool();
        let r = Role::named(RoleId::new(0));
        let a = p.atomic(ClassId::new(0));
        let na = p.not(a);
        let conj = p.and([a, na]);
        assert!(is_nnf(conj, &p));
        let all_na = p.all(r, na);
        assert!(is_nnf(all_na, &p));
    }

    #[test]
    fn is_nnf_rejects_non_atomic_negation() {
        // Not(And(...)) is NOT in NNF.
        let mut p = pool();
        let a = p.atomic(ClassId::new(0));
        let b = p.atomic(ClassId::new(1));
        let conj = p.and([a, b]);
        let neg = p.not(conj);
        assert!(!is_nnf(neg, &p));
    }

    // ── nnf_axioms tests ───────────────────────────────────────────────

    use crate::ontology::{Axiom, InternalOntology};

    fn fresh_ontology() -> InternalOntology {
        let mut o = InternalOntology::new();
        o.vocabulary.intern_class("A");
        o.vocabulary.intern_class("B");
        o.vocabulary.intern_class("C");
        o
    }

    #[test]
    fn nnf_axioms_rewrites_sub_class_of() {
        // SubClassOf(A, Not(And(B, C)))  →  SubClassOf(A, Or(Not(B), Not(C)))
        let mut o = fresh_ontology();
        let a = o.concepts.atomic(ClassId::new(0));
        let b = o.concepts.atomic(ClassId::new(1));
        let c = o.concepts.atomic(ClassId::new(2));
        let and_bc = o.concepts.and([b, c]);
        let not_and = o.concepts.not(and_bc);
        o.axioms.push(Axiom::SubClassOf {
            sub: a,
            sup: not_and,
        });
        let normalized = nnf_axioms(&mut o);
        let Axiom::SubClassOf { sub, sup } = &normalized[0] else {
            panic!()
        };
        assert_eq!(*sub, a);
        let nb = o.concepts.not(b);
        let nc = o.concepts.not(c);
        let expected_sup = o.concepts.or([nb, nc]);
        assert_eq!(*sup, expected_sup);
        assert!(is_nnf(*sup, &o.concepts));
    }

    #[test]
    fn nnf_axioms_leaves_original_axioms_unchanged() {
        // Verify the source list survives unaltered.
        let mut o = fresh_ontology();
        let a = o.concepts.atomic(ClassId::new(0));
        let b = o.concepts.atomic(ClassId::new(1));
        let and_ab = o.concepts.and([a, b]);
        let not_and = o.concepts.not(and_ab);
        o.axioms.push(Axiom::SubClassOf {
            sub: a,
            sup: not_and,
        });
        let original_count = o.axioms.len();
        let _ = nnf_axioms(&mut o);
        assert_eq!(o.axioms.len(), original_count);
        // The original axiom still holds the un-NNF'd super-concept.
        let Axiom::SubClassOf { sup, .. } = &o.axioms[0] else {
            panic!()
        };
        assert_eq!(*sup, not_and);
    }

    #[test]
    fn nnf_axioms_handles_multi_concept_axioms() {
        // EquivalentClasses normalizes every member.
        let mut o = fresh_ontology();
        let a = o.concepts.atomic(ClassId::new(0));
        let b = o.concepts.atomic(ClassId::new(1));
        let not_a = o.concepts.not(a);
        let not_not_a = o.concepts.not(not_a);
        o.axioms.push(Axiom::EquivalentClasses(vec![not_not_a, b]));
        let normalized = nnf_axioms(&mut o);
        let Axiom::EquivalentClasses(ids) = &normalized[0] else {
            panic!()
        };
        // Double-negated A collapses to A in NNF.
        assert_eq!(ids[0], a);
        assert_eq!(ids[1], b);
    }

    #[test]
    fn nnf_axioms_passes_through_role_axioms_unchanged() {
        let mut o = fresh_ontology();
        let r = Role::named(RoleId::new(0));
        o.axioms.push(Axiom::TransitiveRole(r));
        let normalized = nnf_axioms(&mut o);
        assert!(matches!(normalized[0], Axiom::TransitiveRole(_)));
    }

    #[test]
    fn nnf_axioms_normalizes_class_assertion_concept() {
        let mut o = fresh_ontology();
        let i = o.vocabulary.intern_individual("a");
        let a = o.concepts.atomic(ClassId::new(0));
        let not_a = o.concepts.not(a);
        let not_not_a = o.concepts.not(not_a);
        o.axioms.push(Axiom::ClassAssertion {
            class: not_not_a,
            individual: i,
        });
        let normalized = nnf_axioms(&mut o);
        let Axiom::ClassAssertion { class, .. } = normalized[0] else {
            panic!()
        };
        assert_eq!(class, a);
    }
}