Skip to main content

qualia_core_db/modalities/logic/
n3logic.rs

1use crate::q_hash;
2use crate::NQuin;
3
4#[derive(Debug)]
5pub enum N3LogicError {
6    ParseError,
7    HardwareUnavailable(&'static str),
8}
9
10/// N3Logic is the foundational rule-based inference engine.
11/// It reads the raw triples/rules submitted in the Agent Intent
12/// and dynamically infers which specialized logic modalities must be invoked.
13pub fn infer_logic_bindings(quins: &[NQuin]) -> Result<Vec<&'static str>, N3LogicError> {
14    let mut bindings = Vec::new();
15
16    // We strictly enforce Epistemic Logic as the Sandbox Isolator
17    bindings.push("modality:epistemic");
18
19    // We strictly enforce Deontic Logic for human-rights alignment
20    bindings.push("modality:deontic");
21
22    let lock_predicate = q_hash("OP_INTENT_LOCK");
23    let action_payload = q_hash("q42:actionPayload");
24    let cost_predicate = q_hash("q42:burnsTokens");
25    let structural_refactor = q_hash("q42:structuralRefactor");
26
27    // Hardware acceleration and solver routing triggers
28    let uses_pga = q_hash("q42:usesPGA");
29    let quantum_opt = q_hash("q42:quantumOptimize");
30    let solve_calculus = q_hash("q42:solveCalculus");
31    let check_paraconsistent = q_hash("q42:paraconsistentCheck");
32    let spatial_region = q_hash("q42:spatialRegion");
33
34    // Specialized Libraries routing triggers
35    let chem_model = q_hash("q42:chemistryModeling");
36    let phys_sim = q_hash("q42:physicsSimulation");
37    let med_comp = q_hash("q42:medicalComputing");
38    let crypto_lib = q_hash("q42:cryptography");
39
40    for q in quins {
41        let opcode = (q.predicate & 0xFF) as u8;
42
43        // If the agent submits an Intent Lock or modifies the active payload, enforce Temporal Leases
44        if opcode == 0x23 || q.predicate == lock_predicate || q.predicate == action_payload {
45            if !bindings.contains(&"modality:temporal") {
46                bindings.push("modality:temporal");
47            }
48        }
49
50        // If the agent requests a structural refactor, lock the namespace
51        if q.predicate == structural_refactor {
52            if !bindings.contains(&"modality:epistemic:namespace_lock") {
53                bindings.push("modality:epistemic:namespace_lock");
54            }
55        }
56
57        // If the agent burns compute tokens, enforce Defeasible Logic for cost-overrun exceptions
58        if q.predicate == cost_predicate {
59            if !bindings.contains(&"modality:defeasible") {
60                bindings.push("modality:defeasible");
61            }
62        }
63
64        // Geometric Algebra / PGA routing
65        if q.predicate == uses_pga {
66            if !bindings.contains(&"modality:geometric_algebra") {
67                bindings.push("modality:geometric_algebra");
68            }
69        }
70
71        // Hardware gating: the QPU is strictly assumed offline at this stage.
72        if q.predicate == quantum_opt {
73            return Err(N3LogicError::HardwareUnavailable(
74                "QPU offline. Intent requested q42:quantumOptimize.",
75            ));
76        }
77        if q.predicate == solve_calculus {
78            if !bindings.contains(&"modality:solver:calculus") {
79                bindings.push("modality:solver:calculus");
80            }
81        }
82
83        // Advanced Modalities
84        if q.predicate == check_paraconsistent {
85            if !bindings.contains(&"modality:paraconsistent") {
86                bindings.push("modality:paraconsistent");
87            }
88        }
89        if q.predicate == spatial_region {
90            if !bindings.contains(&"modality:spatio_temporal") {
91                bindings.push("modality:spatio_temporal");
92            }
93        }
94
95        // Specialized Libraries
96        if q.predicate == chem_model {
97            if !bindings.contains(&"modality:specialized:chemistry") {
98                bindings.push("modality:specialized:chemistry");
99            }
100        }
101        if q.predicate == phys_sim {
102            if !bindings.contains(&"modality:specialized:physics") {
103                bindings.push("modality:specialized:physics");
104            }
105        }
106        if q.predicate == med_comp {
107            if !bindings.contains(&"modality:specialized:medical") {
108                bindings.push("modality:specialized:medical");
109            }
110        }
111        if q.predicate == crypto_lib {
112            if !bindings.contains(&"modality:specialized:crypto") {
113                bindings.push("modality:specialized:crypto");
114            }
115        }
116    }
117
118    Ok(bindings)
119}