@aleph-ai/tinyaleph 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +609 -13
  2. package/backends/bioinformatics/binding.js +503 -0
  3. package/backends/bioinformatics/dna-computing.js +664 -0
  4. package/backends/bioinformatics/encoding.js +339 -0
  5. package/backends/bioinformatics/folding.js +454 -0
  6. package/backends/bioinformatics/genetic-code.js +269 -0
  7. package/backends/bioinformatics/index.js +522 -0
  8. package/backends/bioinformatics/transcription.js +221 -0
  9. package/backends/bioinformatics/translation.js +264 -0
  10. package/backends/cryptographic/index.js +455 -2
  11. package/backends/index.js +25 -1
  12. package/core/beacon.js +735 -0
  13. package/core/compound.js +532 -0
  14. package/core/crt-homology.js +1004 -0
  15. package/core/enochian-vocabulary.js +910 -0
  16. package/core/enochian.js +744 -0
  17. package/core/errors.js +587 -0
  18. package/core/hilbert.js +1105 -2
  19. package/core/index.js +192 -13
  20. package/core/inference.js +605 -0
  21. package/core/lambda.js +284 -33
  22. package/core/logger.js +350 -0
  23. package/core/prime.js +136 -1
  24. package/core/quaternion-semantics.js +623 -0
  25. package/core/reduction.js +391 -1
  26. package/core/resonance.js +245 -616
  27. package/core/rformer-crt.js +892 -0
  28. package/core/symbols/archetypes.js +478 -0
  29. package/core/symbols/base.js +302 -0
  30. package/core/symbols/elements.js +487 -0
  31. package/core/symbols/hieroglyphs.js +303 -0
  32. package/core/symbols/iching.js +471 -0
  33. package/core/symbols/index.js +77 -0
  34. package/core/symbols/tarot.js +211 -0
  35. package/core/symbols.js +22 -0
  36. package/core/topology.js +655 -0
  37. package/docs/README.md +54 -0
  38. package/docs/design/BIOINFORMATICS_BACKEND_DESIGN.md +493 -0
  39. package/docs/guide/06-symbolic-ai.md +370 -0
  40. package/docs/guide/README.md +2 -1
  41. package/docs/reference/05-symbolic-ai.md +570 -0
  42. package/docs/reference/06-bioinformatics.md +546 -0
  43. package/docs/reference/07-topology.md +257 -0
  44. package/docs/reference/08-observer.md +421 -0
  45. package/docs/reference/09-crt-homology.md +369 -0
  46. package/docs/reference/README.md +32 -2
  47. package/docs/theory/11-prgraph-memory.md +559 -0
  48. package/docs/theory/12-resonant-attention.md +661 -0
  49. package/modular.js +264 -4
  50. package/package.json +1 -1
@@ -0,0 +1,369 @@
1
+ # CRT-Homology Framework
2
+
3
+ ## Overview
4
+
5
+ The CRT-Homology framework integrates **Chinese Remainder Theorem reconstruction** with **homological algebra** to detect and regularize semantic inconsistencies in the tinyaleph library. This enables:
6
+
7
+ 1. **Modular encoding** of semantic states over coprime bases
8
+ 2. **CRT reconstruction** for unique value recovery
9
+ 3. **Birkhoff polytope projection** for doubly-stochastic attention
10
+ 4. **Homology loss** for detecting topological "holes" (consistency failures)
11
+
12
+ ## Mathematical Foundation
13
+
14
+ ### Key Insight
15
+
16
+ > "Holes are not degrees of freedom. Holes are consistency failures that persist under perturbation."
17
+
18
+ In the CRT framework, a semantic "hole" occurs when:
19
+ - Residues `r_k` over coprime moduli `p_k` fail to reconstruct uniquely
20
+ - The reconstruction error `ε(r) = |ℛ(r) - nearest_valid|` exceeds threshold τ
21
+ - These failures form cycles in the kernel `Ker(ℛ)` with non-trivial Betti numbers
22
+
23
+ ### Core Equations
24
+
25
+ #### Residue Encoding
26
+ ```
27
+ r_k = softmax(W_k h + b_k) ∈ Δ(ℤ/p_k)
28
+ ```
29
+
30
+ Each hidden vector `h` is encoded into K probability distributions, one per coprime modulus.
31
+
32
+ #### CRT Reconstruction
33
+ ```
34
+ L̂ = Σ_k E[r_k] · (P/p_k) · (P/p_k)^{-1} mod p_k
35
+ ```
36
+
37
+ Where:
38
+ - `P = ∏_k p_k` (product of all moduli)
39
+ - `(P/p_k)^{-1}` is the modular inverse mod `p_k`
40
+ - `E[r_k]` is the expected residue
41
+
42
+ #### Birkhoff Projection
43
+ ```
44
+ A = Birkhoff(QK^T/√d) ⊙ V
45
+ ```
46
+
47
+ Attention matrices are projected onto the Birkhoff polytope (doubly-stochastic matrices) using Sinkhorn-Knopp iteration.
48
+
49
+ #### Homology Loss
50
+ ```
51
+ ℒ_homology = Σ_{cycles ∈ Ker(ℛ)} f(cycle)
52
+ f(cycle) = Σ_{r ∈ cycle} σ(ε(r) - τ) · |cycle|^α · β^γ
53
+ ```
54
+
55
+ ## API Reference
56
+
57
+ ### Core Classes
58
+
59
+ #### `ResidueEncoder`
60
+
61
+ Encodes hidden vectors into residue distributions over coprime moduli.
62
+
63
+ ```javascript
64
+ const { ResidueEncoder } = require('tinyaleph/core');
65
+
66
+ const primes = [2, 3, 5, 7]; // Coprime moduli
67
+ const hiddenDim = 32;
68
+ const encoder = new ResidueEncoder(primes, hiddenDim);
69
+
70
+ // Encode a hidden vector
71
+ const h = new Float64Array(32);
72
+ h.fill(0.5);
73
+ const residues = encoder.encode(h); // Array of K distributions
74
+
75
+ // Get expected residues
76
+ const expected = encoder.expectedResidues(residues);
77
+ console.log('Expected residues:', expected);
78
+ ```
79
+
80
+ #### `CRTReconstructor`
81
+
82
+ Reconstructs values from residues using Chinese Remainder Theorem.
83
+
84
+ ```javascript
85
+ const { CRTReconstructor } = require('tinyaleph/core');
86
+
87
+ const primes = [2, 3, 5, 7];
88
+ const crt = new CRTReconstructor(primes);
89
+
90
+ // Reconstruct from expected residues
91
+ const residues = [1, 2, 3, 4]; // Expected values mod each prime
92
+ const L = crt.reconstruct(residues);
93
+ console.log('Reconstructed:', L, 'mod', crt.P);
94
+
95
+ // Detect kernel (consistency failure)
96
+ const inKernel = crt.detectKernel(residues, 0.1);
97
+ console.log('In kernel:', inKernel);
98
+
99
+ // Validate residues
100
+ const result = crt.validate(residues);
101
+ console.log('Valid:', result.valid, 'Error:', result.error);
102
+ ```
103
+
104
+ #### `BirkhoffProjector`
105
+
106
+ Projects matrices onto the Birkhoff polytope using Sinkhorn-Knopp.
107
+
108
+ ```javascript
109
+ const { BirkhoffProjector } = require('tinyaleph/core');
110
+
111
+ const projector = new BirkhoffProjector(10); // 10 iterations
112
+
113
+ // Project a matrix to doubly-stochastic
114
+ const matrix = [
115
+ [0.5, 0.3, 0.2],
116
+ [0.3, 0.4, 0.3],
117
+ [0.2, 0.3, 0.5]
118
+ ];
119
+ const doublyStochastic = projector.project(matrix);
120
+
121
+ // Validate result
122
+ const validation = projector.validate(doublyStochastic);
123
+ console.log('Is doubly-stochastic:', validation.isDoublyStochastic);
124
+
125
+ // Birkhoff attention
126
+ const Q = [[1, 0], [0, 1]];
127
+ const K = [[1, 0], [0, 1]];
128
+ const V = [[0.5, 0.5], [0.5, 0.5]];
129
+ const output = projector.attention(Q, K, V);
130
+ ```
131
+
132
+ #### `HomologyLoss`
133
+
134
+ Computes loss terms for obstruction cycles.
135
+
136
+ ```javascript
137
+ const { HomologyLoss, CRTReconstructor } = require('tinyaleph/core');
138
+
139
+ const crt = new CRTReconstructor([2, 3, 5, 7]);
140
+ const homology = new HomologyLoss({
141
+ tau: 0.1, // Kernel detection threshold
142
+ alpha: 0.5, // Cycle length exponent
143
+ beta: 1.0, // Residue weight
144
+ gamma: 0.5 // Residue exponent
145
+ });
146
+
147
+ // Batch of residue tuples
148
+ const batch = [
149
+ [0.1, 0.2, 0.3, 0.4],
150
+ [0.5, 0.6, 0.7, 0.8],
151
+ [0.9, 0.1, 0.2, 0.3]
152
+ ];
153
+
154
+ // Compute homology loss
155
+ const result = homology.compute(batch, crt);
156
+ console.log('Homology loss:', result.loss);
157
+ console.log('Cycles detected:', result.cycles);
158
+
159
+ // Compute Betti numbers
160
+ const betti = homology.computeBettiNumbers(batch, crt);
161
+ console.log('β₀ (components):', betti.beta0);
162
+ console.log('β₁ (holes):', betti.beta1);
163
+ ```
164
+
165
+ #### `CRTModularLayer`
166
+
167
+ Integrated layer combining encoding, reconstruction, and attention.
168
+
169
+ ```javascript
170
+ const { createCRTLayer } = require('tinyaleph/core');
171
+
172
+ const layer = createCRTLayer([2, 3, 5, 7], 32);
173
+
174
+ // Forward pass
175
+ const h = new Float64Array(32);
176
+ h.fill(0.5);
177
+ const result = layer.forward(h);
178
+
179
+ console.log('Latent:', result.latent);
180
+ console.log('In kernel:', result.inKernel);
181
+ console.log('Coherence:', result.coherence);
182
+
183
+ // Batch forward with homology loss
184
+ const batch = [h, h, h];
185
+ const batchResult = layer.forwardBatch(batch);
186
+ console.log('Homology loss:', batchResult.homologyLoss);
187
+ console.log('Betti numbers:', batchResult.bettiNumbers);
188
+ ```
189
+
190
+ ### CRT-Enhanced ResoFormer
191
+
192
+ #### `CRTResonantAttention`
193
+
194
+ Multi-head attention with per-modulus Birkhoff projection.
195
+
196
+ ```javascript
197
+ const { CRTResonantAttention } = require('tinyaleph/core');
198
+
199
+ const attention = new CRTResonantAttention({
200
+ numHeads: 4,
201
+ numPrimes: 4096,
202
+ activeK: 32,
203
+ sinkhornIterations: 10
204
+ });
205
+
206
+ // Forward pass
207
+ const query = SparsePrimeState.fromHash('query');
208
+ const keys = [SparsePrimeState.fromHash('key1'), SparsePrimeState.fromHash('key2')];
209
+ const values = [SparsePrimeState.fromHash('val1'), SparsePrimeState.fromHash('val2')];
210
+
211
+ const result = attention.forward(query, keys, values);
212
+ console.log('Result:', result.result);
213
+ console.log('Homology info:', result.homologyInfo);
214
+ console.log('Has holes:', result.homologyInfo.hasHoles);
215
+ ```
216
+
217
+ #### `CRTResoFormer`
218
+
219
+ Complete CRT-enhanced ResoFormer model.
220
+
221
+ ```javascript
222
+ const { createCRTResoFormer, SparsePrimeState } = require('tinyaleph/core');
223
+
224
+ const model = createCRTResoFormer({
225
+ numLayers: 6,
226
+ numHeads: 8,
227
+ hiddenDim: 256,
228
+ numPrimes: 4096,
229
+ activeK: 32,
230
+ homologyWeight: 0.1
231
+ });
232
+
233
+ // Single input
234
+ const input = SparsePrimeState.fromHash('hello world');
235
+ const output = model.forward(input);
236
+
237
+ console.log('Output:', output.output);
238
+ console.log('Total homology loss:', output.totalLoss);
239
+ console.log('Holes detected:', output.homologyReport.totalHolesDetected);
240
+
241
+ // Sequence input
242
+ const sequence = [
243
+ SparsePrimeState.fromHash('the'),
244
+ SparsePrimeState.fromHash('quick'),
245
+ SparsePrimeState.fromHash('brown'),
246
+ SparsePrimeState.fromHash('fox')
247
+ ];
248
+ const seqOutput = model.forward(sequence);
249
+ console.log('Sequence output length:', seqOutput.output.length);
250
+ ```
251
+
252
+ ### Homology-Aware Stabilization
253
+
254
+ The `StabilizationController` in `observer/hqe.js` now includes homology detection:
255
+
256
+ ```javascript
257
+ const { StabilizationController } = require('tinyaleph/observer/hqe');
258
+
259
+ const controller = new StabilizationController({
260
+ lambda0: 0.1,
261
+ aC: 1.0, // Coherence weight
262
+ aS: 0.8, // Entropy weight
263
+ aH: 0.3, // Homology weight (NEW)
264
+ homology: {
265
+ enabled: true,
266
+ numModuli: 4,
267
+ tau: 0.1
268
+ }
269
+ });
270
+
271
+ // Compute λ(t) with homology awareness
272
+ const lambda = controller.computeLambda(
273
+ 0.8, // coherence
274
+ 0.3, // entropy
275
+ 0.1, // SMF entropy
276
+ { coherence: 0.8, entropy: 0.3 } // state for homology analysis
277
+ );
278
+
279
+ console.log('Lambda:', lambda);
280
+ console.log('Betti numbers:', controller.getHomologyState().bettiNumbers);
281
+ console.log('Holes detected:', controller.getHomologyState().holesDetected);
282
+ ```
283
+
284
+ ## Integration with Lyapunov Stability
285
+
286
+ The CRT kernel detection correlates with Lyapunov instability:
287
+
288
+ - **λ > 0** (unstable): High reconstruction error, in kernel
289
+ - **λ ≈ 0** (marginal): Near threshold, transitional
290
+ - **λ < 0** (stable): Low error, consistent reconstruction
291
+
292
+ ```javascript
293
+ const { lyapunovExponent } = require('tinyaleph/physics');
294
+ const { CRTReconstructor } = require('tinyaleph/core');
295
+
296
+ const crt = new CRTReconstructor([2, 3, 5, 7]);
297
+
298
+ // Track correlation between Lyapunov and CRT error
299
+ function correlateStability(trajectory) {
300
+ const correlations = [];
301
+
302
+ for (const state of trajectory) {
303
+ const lambda = lyapunovExponent(state);
304
+ const residues = extractResidues(state);
305
+ const crtError = crt.reconstructionError(residues);
306
+
307
+ correlations.push({
308
+ lambda,
309
+ crtError,
310
+ inKernel: crtError > 0.1
311
+ });
312
+ }
313
+
314
+ return correlations;
315
+ }
316
+ ```
317
+
318
+ ## Theoretical Background
319
+
320
+ ### Chinese Remainder Theorem
321
+
322
+ For pairwise coprime moduli `p₁, p₂, ..., p_k`:
323
+
324
+ 1. Any tuple `(r₁, r₂, ..., r_k)` with `0 ≤ r_i < p_i` corresponds to a unique integer `x ∈ [0, P)` where `P = ∏ p_i`
325
+ 2. Reconstruction uses: `x = Σ_i r_i · M_i · M_i^{-1} mod P`
326
+ 3. Where `M_i = P/p_i` and `M_i^{-1}` is the modular inverse
327
+
328
+ ### Birkhoff-von Neumann Theorem
329
+
330
+ Every doubly-stochastic matrix is a convex combination of permutation matrices:
331
+
332
+ 1. Row sums = 1, column sums = 1
333
+ 2. Birkhoff polytope = convex hull of permutation matrices
334
+ 3. Sinkhorn-Knopp alternates row/column normalization
335
+
336
+ ### Homology and Betti Numbers
337
+
338
+ - **β₀**: Number of connected components in kernel
339
+ - **β₁**: Number of 1-dimensional holes (cycles that don't bound)
340
+ - Persistence: How long holes survive under parameter changes
341
+
342
+ ## Performance Considerations
343
+
344
+ ### Coprime Selection
345
+
346
+ Use small primes for efficiency:
347
+ - `[2, 3, 5, 7]`: P = 210, good for most applications
348
+ - `[5, 7, 11, 13]`: P = 5005, for larger latent spaces
349
+ - `[2, 3, 5, 7, 11]`: P = 2310, semantic applications
350
+
351
+ ### Sinkhorn Iterations
352
+
353
+ - 5-10 iterations suffice for most applications
354
+ - Higher tolerance (1e-3) for speed, lower (1e-6) for precision
355
+ - Monitor convergence via `maxRowError` and `maxColError`
356
+
357
+ ### Homology Computation
358
+
359
+ - Cycle detection scales with kernel size
360
+ - Betti number computation is approximate (not full persistent homology)
361
+ - Consider batching for large sequences
362
+
363
+ ## References
364
+
365
+ 1. Chinese Remainder Theorem - Hardy & Wright, "An Introduction to the Theory of Numbers"
366
+ 2. Birkhoff-von Neumann Theorem - Birkhoff, "Three Observations on Linear Algebra"
367
+ 3. Sinkhorn-Knopp Algorithm - Sinkhorn, "A Relationship Between Arbitrary Positive Matrices and Doubly Stochastic Matrices"
368
+ 4. Topological Data Analysis - Carlsson, "Topology and Data"
369
+ 5. Lyapunov Stability - Strogatz, "Nonlinear Dynamics and Chaos"
@@ -35,6 +35,20 @@ Domain-specific computation engines:
35
35
  - **SemanticBackend** - Natural language and concept processing
36
36
  - **CryptographicBackend** - Hashing and key derivation
37
37
  - **ScientificBackend** - Quantum-inspired computation
38
+ - **BioinformaticsBackend** - DNA/RNA/Protein computation
39
+
40
+ ### [Bioinformatics Module](./06-bioinformatics.md)
41
+
42
+ DNA computing and molecular biology:
43
+
44
+ - **BioinformaticsBackend** - DNA/RNA/Protein encoding and operations
45
+ - **Transcription** - DNA to RNA conversion (Central Dogma)
46
+ - **Translation** - RNA to Protein via genetic code
47
+ - **FoldingTransform** - Protein folding via Kuramoto oscillators
48
+ - **DNACircuit** - DNA logic circuit construction
49
+ - **ANDGate / ORGate / NOTGate / NANDGate** - DNA logic gates
50
+ - **StrandDisplacementReaction** - Toehold-mediated displacement
51
+ - **BindingAffinityCalculator** - Molecular binding scoring
38
52
 
39
53
  ### [Engine Module](./04-engine.md)
40
54
 
@@ -44,7 +58,16 @@ High-level orchestration:
44
58
  - **createEngine** - Factory function for engine creation
45
59
  - **registerBackend** - Backend registration system
46
60
 
47
- ### [Formal Semantics Module](./05-formal-semantics.md)
61
+ ### [Symbolic AI Module](./05-symbolic-ai.md)
62
+
63
+ Symbol inference and resonance attention:
64
+
65
+ - **SymbolDatabase** - 184+ emoji symbols with prime assignments and cultural tags
66
+ - **SemanticInference** - Pattern matching and resonance-enhanced text→symbol mapping
67
+ - **CompoundBuilder** - Multi-symbol concept composition
68
+ - **ResonanceCalculator** - Golden ratio-based harmony measurement
69
+
70
+ ### [Formal Semantics Module](./06-formal-semantics.md)
48
71
 
49
72
  Formal type system and reduction semantics:
50
73
 
@@ -55,7 +78,7 @@ Formal type system and reduction semantics:
55
78
  - **LambdaEvaluator** - β-reduction and normalization
56
79
  - **Semantics** - Model-theoretic interpretation
57
80
 
58
- ### [Enochian Module](./06-enochian.md)
81
+ ### [Enochian Module](./07-enochian.md)
59
82
 
60
83
  The angelic language system:
61
84
 
@@ -269,6 +292,10 @@ console.assert(coherence >= 0 && coherence <= 1);
269
292
  | types | `core/types.js` | Formal type system |
270
293
  | reduction | `core/reduction.js` | Reduction semantics |
271
294
  | lambda | `core/lambda.js` | λ-calculus translation |
295
+ | symbols | `core/symbols.js` | Symbol database |
296
+ | inference | `core/inference.js` | Semantic inference |
297
+ | compound | `core/compound.js` | Compound builder |
298
+ | resonance | `core/resonance.js` | Resonance calculator |
272
299
  | oscillator | `physics/oscillator.js` | Oscillator creation |
273
300
  | kuramoto | `physics/kuramoto.js` | Synchronization |
274
301
  | entropy | `physics/entropy.js` | Information theory |
@@ -277,6 +304,9 @@ console.assert(coherence >= 0 && coherence <= 1);
277
304
  | semantic | `backends/semantic/index.js` | Semantic backend |
278
305
  | cryptographic | `backends/cryptographic/index.js` | Crypto backend |
279
306
  | scientific | `backends/scientific/index.js` | Science backend |
307
+ | bioinformatics | `backends/bioinformatics/index.js` | Bioinformatics backend |
308
+ | dna-computing | `backends/bioinformatics/dna-computing.js` | DNA logic gates/circuits |
309
+ | folding | `backends/bioinformatics/folding.js` | Protein folding dynamics |
280
310
  | engine | `engine/aleph.js` | Main engine |
281
311
  | enochian | `apps/sentient/lib/enochian-vocabulary.js` | Enochian language |
282
312