inference_engine/reasoning/mod.rs
1//! Bounded forward execution of shared positive RDF/RDFS structural clauses.
2//!
3//! Callers own immutable selected input, indexed scratch storage and publication.
4//! This module never collects a graph, chooses graphs, or promotes partial output.
5
6mod conflict;
7mod context;
8mod engine;
9mod literal;
10mod literal_typing;
11mod occurrence;
12mod occurrence_typing;
13mod plan;
14mod proposition;
15mod seeding;
16mod source;
17mod target;
18mod types;
19
20pub use conflict::{
21 DatatypeConflict, DatatypeConflictError, DatatypeConflictLimits, DatatypeConflictReport,
22 DatatypeConflictSink, Rdf12DatatypeConflictChecker,
23};
24pub use context::{
25 Rdfs12Context, Rdfs12ContextConfig, Rdfs12ContextError, Rdfs12ContextOutcome,
26 Rdfs12TargetConfig, Rdfs12TargetReport,
27};
28pub use engine::Reasoner;
29pub use literal::{
30 LexicalViolation, LiteralError, LiteralInspection, LiteralReader, LiteralReport,
31 Rdf12LiteralValidator,
32};
33pub use literal_typing::{
34 LiteralCursorError, LiteralOccurrence, LiteralOccurrenceCursor, LiteralOccurrenceRead,
35 LiteralRole, LiteralTermAccess, LiteralTypingError, LiteralTypingLimits, LiteralTypingReport,
36 Rdf12LiteralOccurrences, Rdf12LiteralTyper,
37};
38pub use occurrence::{
39 OccurrenceError, OccurrenceLimits, OccurrencePath, Rdf12OccurrenceCursor, TermOccurrence,
40 TermPosition,
41};
42pub use occurrence_typing::{OccurrenceTypingError, OccurrenceTypingReport, Rdf12OccurrenceTyper};
43pub use proposition::{PropositionLimits, PropositionReport, Rdf12PropositionConstructor};
44pub use seeding::Rdf12AxiomSeeder;
45pub use source::{
46 Rdf12ContainerAccess, Rdf12SourceDiagnostic, Rdf12SourceDiagnosticSink, Rdfs12Source,
47 Rdfs12SourceConfig, Rdfs12SourceError, Rdfs12SourceOutcome, Rdfs12SourceReport,
48 SourceLiteralLimits,
49};
50pub use target::{
51 Rdf12TargetConstructor, TargetConstructionError, TargetConstructionLimits,
52 TargetConstructionReport,
53};
54pub use types::{
55 AxiomFamily, Evidence, ExecutionProfile, Inference, Limit, Premises, ProfileError,
56 ReasoningError, RunLimits, RunReport,
57};
58
59use std::{error::Error, future::Future};
60
61/// A fallible, bounded cursor over an immutable snapshot's matching tuples.
62pub trait TripleCursor: Send {
63 /// Caller-owned exact RDF term identity; scoped blanks must remain distinct.
64 type Term: Clone + Eq + Send + Sync;
65 /// Read failures, including cancellation, must never become end of stream.
66 type Error: Error + Send + Sync + 'static;
67 /// Read at most one tuple. Drop must release/cancel the underlying read.
68 fn next(&mut self)
69 -> impl Future<Output = Result<Option<[Self::Term; 3]>, Self::Error>> + Send;
70}
71
72/// One immutable round view of selected assertions plus staged consequences.
73///
74/// Scans must be bounded and match every supplied term exactly. The view and
75/// its cursors remain valid while the caller inserts into the separate store.
76/// No ordering is required; set closure is independent of scan order. The first
77/// retained proof can differ between orders and is not a canonical explanation.
78pub trait TripleSource: Send + Sync {
79 /// Exact term type, without lexical/value normalization by the executor.
80 type Term: Clone + Eq + Send + Sync;
81 /// Backend read error.
82 type Error: Error + Send + Sync + 'static;
83 /// Owned cursor, independent of subsequent store writes.
84 type Cursor: TripleCursor<Term = Self::Term, Error = Self::Error>;
85 /// Open a scan/probe; `None` is a wildcard in subject/predicate/object order.
86 fn scan(
87 &self,
88 pattern: [Option<Self::Term>; 3],
89 ) -> impl Future<Output = Result<Self::Cursor, Self::Error>> + Send;
90}
91
92/// Backend capabilities for one fresh, isolated inference attempt.
93///
94/// Initially expose only the caller's selected asserted snapshot, never earlier
95/// inferred output. Explicit axiom seeding may precede rule execution within
96/// the same fresh attempt. Scratch facts must spill to caller-owned indexed storage;
97/// no method may buffer a graph-sized relation. Each snapshot freezes the union
98/// as of its creation; subsequent insertions are visible in the next snapshot.
99///
100/// On any error the caller must discard/abort staged output. A successful run
101/// grants no publication authority: callers still check input currentness and
102/// atomically promote their owned inference contribution. Recompute from fresh
103/// assertions after deletion. The executor never mutates asserted input.
104pub trait ReasoningStore: Send {
105 /// Exact RDF term identity; database IDs and backend RDF terms both work.
106 type Term: Clone + Eq + Send + Sync;
107 /// Backend failure propagated without substituting an empty relation.
108 type Error: Error + Send + Sync + 'static;
109 /// Owned immutable view supporting concurrent outer/inner read cursors.
110 type Snapshot: TripleSource<Term = Self::Term, Error = Self::Error>;
111 /// Resolve a full rule-constant IRI as an IRI, never as a plain string literal.
112 fn resolve_iri(
113 &mut self,
114 iri: &str,
115 ) -> impl Future<Output = Result<Self::Term, Self::Error>> + Send;
116 /// Freeze selected assertions plus all currently staged inferred tuples.
117 fn snapshot(&mut self) -> impl Future<Output = Result<Self::Snapshot, Self::Error>> + Send;
118 /// Indexed membership in the current asserted-plus-staged union.
119 fn contains(
120 &mut self,
121 triple: &[Self::Term; 3],
122 ) -> impl Future<Output = Result<bool, Self::Error>> + Send;
123 /// Stage a novel inferred tuple with its first proof; assertions win duplicates.
124 ///
125 /// Return `true` only for a new inferred tuple. Preserve generalized tuples
126 /// in scratch; do not silently drop terms or normalize literal lexical forms.
127 fn insert_inferred(
128 &mut self,
129 inference: Inference<Self::Term>,
130 ) -> impl Future<Output = Result<bool, Self::Error>> + Send;
131 /// Whether this attempt is cancelled/superseded. Awaited operations must also
132 /// support cancellation through the backend's own error/control boundary.
133 fn is_cancelled(&self) -> bool;
134}
135
136/// Caller-owned fresh blank allocation for standard nonempty-class requirements.
137///
138/// Keep bindings in the isolated attempt's storage, keyed by semantic descriptor
139/// and immutable scope. Allocation is independent of lexical representatives or
140/// user-visible IRI minting; unsupported blank allocation must fail explicitly.
141pub trait Rdf12WitnessStore: ReasoningStore {
142 /// Resolve one fresh scoped RDF blank witness, reusing the persisted key for
143 /// repeated calls with the same descriptor in this attempt. Standardize it
144 /// apart from input and target blanks and witnesses in different attempts.
145 /// Never return a fixed user IRI, Skolem IRI or chosen literal value.
146 fn resolve_existential_blank(
147 &mut self,
148 id: rdf_reasoning::Rdf12ExistentialAxiomId,
149 ) -> impl Future<Output = Result<Self::Term, Self::Error>> + Send;
150}
151
152/// Explicit generalized triple-term construction in the caller's dictionary.
153pub trait Rdf12TripleTermStore: ReasoningStore {
154 /// Resolve a canonical subject/predicate/object triple term without asserting
155 /// its contents. Preserve generalized keys, including literal predicates.
156 /// Enforce actual substituted nesting and dictionary/resource limits here.
157 /// Unsupported terms or exhausted limits must return an error, never a
158 /// coerced term, omitted fact or alternate IRI representation.
159 fn resolve_triple_term(
160 &mut self,
161 triple: [Self::Term; 3],
162 ) -> impl Future<Output = Result<Self::Term, Self::Error>> + Send;
163}