Skip to main content

qualia_core_db/modalities/spatiotemporal_linguistics/
mod.rs

1//! Spatio-Temporal Semiotic & Linguistic Informatics Layer (ST-SLIL).
2//!
3//! Provides medium-agnostic linguistic grounding across oral, phonetic, written,
4//! glyphic, pictorial, heraldic, and liturgical wave-field expressions.
5//!
6//! Submodules:
7//! - [`wave_tensor`]: Wave-native, zero-Unicode physical wave-field containers & TimeFrequencyBox.
8//! - [`compositional_semiotics`]: Heraldic blazon AST mereology & toponymic cadastral linking.
9//! - [`drift_engine`]: Historical metrology, diachronic nosology, and spatiotemporal lect (`sl:Lect`) synthesis.
10//! - [`epistemic_firewall`]: De-tokenized intent pre-compilation, anti-folk-etymology oracle, and custodial fiduciary gate.
11//!
12//! Reference: `docs/plans/holo/Spatio-Temporal Linguistics Informatics Framework.md`
13
14pub mod wave_tensor;
15pub mod compositional_semiotics;
16pub mod drift_engine;
17pub mod epistemic_firewall;
18
19pub use wave_tensor::*;
20pub use compositional_semiotics::*;
21pub use drift_engine::*;
22pub use epistemic_firewall::*;
23
24use crate::NQuin;
25
26/// Modality Opcodes (0x80–0x8B adhering to AGENTS.md Immovable Rules)
27pub const OP_SEMIOTIC_SIGN_BIND: u8 = 0x80;        // sl:signifiesInContext
28pub const OP_COGNATE_OF: u8 = 0x81;                // sl:cognateOf
29pub const OP_BORNE_BY_LINEAGE: u8 = 0x82;          // sl:borneByLineage
30pub const OP_ATTESTED_IN_ARCHIVE: u8 = 0x83;       // sl:attestedIn
31pub const OP_NAMING_ACT_CADASTRE: u8 = 0x84;       // sl:NamingAct linking to person/parcel
32pub const OP_BIOREGIONAL_TAXON: u8 = 0x85;         // sl:bioregionalTaxon disambiguation
33pub const OP_SPATIOTEMPORAL_LECT: u8 = 0x86;       // sl:Lect definition
34pub const OP_WAVE_FIELD_CARRIER: u8 = 0x87;        // sl:WaveFieldCarrier time-frequency box
35pub const OP_HERALDIC_AST_NODE: u8 = 0x88;         // sl:HeraldicAchievement element
36pub const OP_METROLOGY_RESOLUTION: u8 = 0x89;      // st:MetrologyLayer unit drift
37pub const OP_NOSOLOGY_BRIDGE: u8 = 0x8A;           // st:ScientificNomenclatureLayer
38pub const OP_CUSTODIAL_RIGHTS: u8 = 0x8B;          // hr:CustodialScope fiduciary gate
39
40/// Compiles a spatio-temporal linguistic or semiotic assertion into a 48-byte `NQuin`.
41#[inline]
42pub fn compile_semiotic_quin(
43    expression_node_id: u64,
44    predicate_hash: u64,
45    opcode: u8,
46    referent_concept_id: u64,
47    envelope_context_id: u64,
48    carrier_modality: u16,
49    lamport_clock: u32,
50) -> NQuin {
51    let predicate = ((predicate_hash & 0x007F_FFFF_FFFF_FFFF) << 8) | (opcode as u64);
52    let metadata = ((lamport_clock as u64 & 0x1FFF_FFFF) << 32)
53        | ((carrier_modality as u64) << 16)
54        | (opcode as u64);
55    let parity = expression_node_id ^ predicate ^ referent_concept_id ^ envelope_context_id;
56
57    NQuin {
58        subject: expression_node_id,
59        predicate,
60        object: referent_concept_id,
61        context: envelope_context_id,
62        metadata,
63        parity,
64    }
65}
66
67/// Disambiguates an ambiguous vernacular plant/animal name (e.g. "Ash" or "Magpie")
68/// against a bioregional species presence polygon and author dialect/lect.
69#[inline]
70pub fn evaluate_bioregional_taxon_disambiguation(
71    vernacular_name_hash: u64,
72    is_australian_bioregion: bool,
73    taxon_australia_hash: u64,
74    taxon_europe_hash: u64,
75) -> u64 {
76    let _ = vernacular_name_hash;
77    if is_australian_bioregion {
78        taxon_australia_hash
79    } else {
80        taxon_europe_hash
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn test_wave_tensor_block_and_time_frequency_addressing() {
90        assert_eq!(std::mem::size_of::<WaveTensorBlockHeader>(), 64);
91        let mut header = WaveTensorBlockHeader::default();
92        header.geodesic_h3_cell = 0x88be2040c1fffff;
93        assert!(header.validate().is_ok());
94
95        // Rising tone / cantillation slice
96        let tf_box = TimeFrequencyBox {
97            start_nanos: 1_000_000,
98            end_nanos: 2_500_000,
99            min_freq_hz: 220.0,
100            max_freq_hz: 880.0,
101            harmonic_mask: 0x0F,
102            _padding: 0,
103        };
104
105        let token = tf_box.to_concept_token(header.geodesic_h3_cell);
106        assert_ne!(token, 0);
107
108        let matching_box = TimeFrequencyBox {
109            start_nanos: 1_500_000,
110            end_nanos: 3_000_000,
111            min_freq_hz: 440.0,
112            max_freq_hz: 660.0,
113            harmonic_mask: 0x0B,
114            _padding: 0,
115        };
116
117        assert!(tf_box.overlaps(&matching_box));
118        let resonance = tf_box.resonance_score(&matching_box);
119        assert!(resonance > 0.0 && resonance <= 1.0);
120    }
121
122    #[test]
123    fn test_heraldic_blazon_mereology_and_context_resolution() {
124        // City of London Arms: Argent a cross Gules, in the first quarter a sword in pale point upwards
125        let sword_element = HeraldicElement {
126            division: HeraldicDivision::PartyPerCross as u8,
127            tincture: HeraldicTincture::Argent as u8,
128            ordinary: HeraldicOrdinary::Cross as u8,
129            cadency_mark: HeraldicCadency::None as u8,
130            marshalling: HeraldicMarshalling::None as u8,
131            _reserved: [0; 3],
132            charge_hash: 0x5357_4F52_44, // "SWORD"
133            meaning_concept_hash: 0x5354_5F50_4155_4C, // "ST_PAUL"
134        };
135
136        let struct_hash = sword_element.structural_hash();
137        assert_ne!(struct_hash, 0);
138
139        let st_paul_hash = 0x1111_0000;
140        let wat_tyler_hash = 0x2222_0000;
141
142        // Canonical institutional resolution:
143        let canonical_meaning = sword_element.resolve_situated_meaning(
144            0x4C4F_4E44_4F4E, // London
145            1600.0,
146            true, // canonical
147            st_paul_hash,
148            wat_tyler_hash,
149        );
150        assert_eq!(canonical_meaning, st_paul_hash);
151
152        // Vernacular folklore resolution:
153        let folklore_meaning = sword_element.resolve_situated_meaning(
154            0x4C4F_4E44_4F4E,
155            1600.0,
156            false, // folklore
157            st_paul_hash,
158            wat_tyler_hash,
159        );
160        assert_eq!(folklore_meaning, wat_tyler_hash);
161    }
162
163    #[test]
164    fn test_cadastral_naming_act_temporal_plausibility() {
165        let naming_act = CadastralNamingAct {
166            toponym_hash: 0xCAFE_0001,
167            bearer_lineage_hash: 0xBEEF_0002,
168            parcel_did_hash: 0xDEED_0003,
169            naming_year: 1885.0,
170            grant_year: 1860.0,
171        };
172        assert!(naming_act.is_temporally_plausible());
173        assert_eq!(naming_act.match_strength(0xBEEF_0002, 0xDEED_0003), 1.0);
174        assert_eq!(naming_act.match_strength(0xBEEF_0002, 0x0000), 0.6);
175    }
176
177    #[test]
178    fn test_metrology_thermal_drift_and_nosology() {
179        // Surveyor chain expansion on hot day (38°C) vs standard (20°C)
180        let area_standard = evaluate_metrology_area_sq_m(MetrologyUnitKind::AcreStatute, 50.0, 20.0);
181        let area_hot = evaluate_metrology_area_sq_m(MetrologyUnitKind::AcreStatute, 50.0, 38.0);
182        assert!(area_hot > area_standard);
183
184        // Winchester vs Imperial bushel volume
185        let vol_winchester = evaluate_metrology_volume_liters(MetrologyUnitKind::BushelWinchester, 10.0);
186        let vol_imperial = evaluate_metrology_volume_liters(MetrologyUnitKind::BushelImperial, 10.0);
187        assert!(vol_imperial > vol_winchester);
188
189        // Nosological translation of "dropsy"
190        let nosology = translate_historical_nosology(HistoricalNosologyTerm::Dropsy, 1880.0);
191        assert_eq!(nosology.primary_concept_hash, 0x0001_0000_0001); // Congestive Heart Failure
192        assert!((nosology.primary_probability - 0.52).abs() < 1e-4);
193    }
194
195    #[test]
196    fn test_spatiotemporal_lect_synthesis() {
197        let lect = SpatioTemporalLect {
198            lect_id_hash: 0x1000,
199            base_dialect_hash: 0x2000,
200            indigenous_substrate_hash: 0x3000,
201            statutory_register_hash: 0x4000,
202            occupational_vocab_hash: 0x5000,
203            spatial_h3_cell: 0x88be2040c1fffff,
204            era_start_year: 1860.0,
205            era_end_year: 1910.0,
206        };
207
208        assert!(lect.is_active_at(1890.0, 0x88be2040c1fffff));
209        assert!(!lect.is_active_at(1950.0, 0x88be2040c1fffff));
210        assert_ne!(lect.composite_fingerprint(), 0);
211    }
212
213    #[test]
214    fn test_epistemic_firewall_and_empirical_reality_check() {
215        // Holborn test: hollow stream valley vs hill fort
216        // Scenario 1: Hydrographic origin in low elevation with alluvial stream -> passes
217        assert!(evaluate_empirical_reality_check(
218            15.0,  // 15m elevation in Fleet valley
219            true,  // alluvial stream present
220            false, // no hill fort
221            false, // not asserting hill fort
222            true,  // asserting hydrographic hollow stream
223        ));
224
225        // Scenario 2: Claiming hill fort on a 15m alluvial valley with no archaeology -> rejected
226        assert!(!evaluate_empirical_reality_check(
227            15.0,
228            true,
229            false, // no hill fort archaeology
230            true,  // asserting hill fort
231            false,
232        ));
233
234        // De-tokenized intent pre-compilation
235        let intent_quin = precompile_detokenized_intent(
236            0x1234,
237            0x5678,
238            0x9ABC,
239            0xDEF0,
240            OP_SEMIOTIC_SIGN_BIND,
241        );
242        assert_eq!(intent_quin.subject, 0x5678);
243        assert_eq!((intent_quin.predicate & 0xFF) as u8, OP_SEMIOTIC_SIGN_BIND);
244    }
245
246    #[test]
247    fn test_compile_semiotic_quin() {
248        let quin = compile_semiotic_quin(
249            0x0001_0002_0003_0004,
250            0x000A_000B_000C_000D,
251            OP_SEMIOTIC_SIGN_BIND,
252            0x0005_0006_0007_0008,
253            0x0009_0009_0009_0009,
254            CARRIER_SONIC_LITURGICAL,
255            128,
256        );
257
258        assert_eq!(quin.subject, 0x0001_0002_0003_0004);
259        assert_eq!((quin.predicate & 0xFF) as u8, OP_SEMIOTIC_SIGN_BIND);
260        assert_eq!(quin.object, 0x0005_0006_0007_0008);
261        assert_eq!(
262            quin.parity,
263            quin.subject ^ quin.predicate ^ quin.object ^ quin.context
264        );
265    }
266}