neberu 0.0.0

Exact geometric algebra from the balanced ternary axiom. Governed rewriting, self-certifying canonicalization via the Kase Optimality Theorem.
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
use neberu::encoding::{certificate_bytes, compute_q2, fmt_hex, pack_word, trit_word_display};
use neberu::expr::Expr;
use neberu::gen::Gen;
use neberu::governance::Governance;
use neberu::rat::Rat;
use neberu::trace::{verify_kot, walk_from_trace};
use neberu::trit::P as TRIT_P;
use neberu::word::Word;

fn main() {
    // ── The computation ───────────────────────────────────────
    let ei0 = Gen::imaginary(0);
    let gov = Governance::cl(1, 0, 0);
    let expr = Expr::term(Rat::one(), Word::from_gens(&[ei0, ei0]));

    let (terminal, trace) = gov.canonicalize_traced(&expr);
    let walk = walk_from_trace(&trace, &gov);
    let kot = verify_kot(&walk, &expr, &gov);
    let q1 = kot.as_trit();
    let q2 = compute_q2(&kot);

    // ── The numbers ───────────────────────────────────────────
    //
    // N₁ — minimal trace encoding.
    // Encode only the informative content: the matched generator types.
    // Rule index (0) and start position (0) are implicit for a 1-rule,
    // 1-position algebra — there is nothing else they could be.
    let step = walk.steps().first().expect("exactly one rewrite step");
    let n1_val = pack_word(&step.matched); // pack_word([N,N]) = 5
    let n1_bits = step.matched.grade() * 2; // 2 trits × 2 bits = 4 bits

    // N₂ — minimal complete KOT governance encoding.
    // The governance has one rule: [P,P,P] → P (closed-world: all else fails).
    // [P,P,P] packed: trit0=P=0b10, trit1=P<<2, trit2=P<<4 → 2|8|32 = 42
    // Verdict trit P = 0b10. Concatenated: 42 | (2 << 6) = 170.
    let n2_src_bits: usize = 6; // 3-trit source word
    let p_bits = TRIT_P.bits() as u128;
    let passing_cert: u128 = p_bits | (p_bits << 2) | (p_bits << 4); // = 42
    let n2_val = passing_cert | (p_bits << n2_src_bits); // = 170
    let n2_bits = n2_src_bits + 2; // 4 trits × 2 bits = 8 bits

    let n3_bytes = certificate_bytes(n1_val, n1_bits, n2_val, n2_bits);
    let n3_val = n3_bytes
        .iter()
        .rev()
        .fold(0u128, |acc, &b| (acc << 8) | b as u128);
    let q1_bits = q1.bits();
    let q2_bits = q2.bits();
    // Logic chain for Q₁ and Q₂: Trit → signed integer → bit encoding → decimal.
    // P is the positive trit: means +1, bit encoding 0b10, decimal 2.
    let q_chain = match q1.bits() {
        0b10 => "P  (== +1 == 0b10 == 2)",
        0b01 => "N  (== -1 == 0b01 == 1)",
        0b00 => "Z  (==  0 == 0b00 == 0)",
        _ => "INV",
    };

    // ── Display ───────────────────────────────────────────────
    let bar = "".repeat(60);

    println!("nēberu  v0.0.0");
    println!("(potentially)_optimal_kase!PROOF!.neb");
    println!();
    println!("╔════════════════════════════════════════════════════════════╗");
    println!("║                                                            ║");
    println!("║        K A S E   O P T I M A L I T Y   T H E O R E M     ║");
    println!("║                                                            ║");
    println!("║  this program's a proof of correct computation,           ║");
    println!("║  proving then the proof's correct.                        ║");
    println!("║                                                            ║");
    println!("╚════════════════════════════════════════════════════════════╝");
    println!();
    println!("  This program proves that a computation was performed correctly.");
    println!("  It then proves that the proof is correct.");
    println!("  Every number below is exact. Nothing is approximated.");

    // ── AXIOM ─────────────────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE AXIOM");
    println!("{}", bar);
    println!();
    println!("  The entire system is built from one rule.");
    println!();
    println!("  A value can be one of three things:");
    println!("    N  =  negative  (-1)   stored as  01");
    println!("    Z  =  zero       (0)   stored as  00");
    println!("    P  =  positive  (+1)   stored as  10");
    println!();
    println!("  When two values multiply, the sign follows one rule:");
    println!("    N · N  =  P       (negative × negative = positive)");
    println!("    N · P  =  N       (negative × positive = negative)");
    println!("    P · P  =  P       (positive × positive = positive)");
    println!("    Z · anything  =  Z   (zero kills everything)");
    println!();
    println!("  That is the complete axiom. Everything below derives from it.");

    // ── COMPUTATION ───────────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE COMPUTATION");
    println!("{}", bar);
    println!();
    println!("  We compute:  e1 · e1   in the algebra  Cl(1,0,0)");
    println!();
    println!("  e1 is a geometric basis vector — the kind that squares to -1.");
    println!("  (This is the same as the imaginary unit i in complex numbers.)");
    println!("  The algebra Cl(1,0,0) has exactly one such vector.");
    println!();
    println!("  The rule for this algebra:");
    println!("    e1 · e1  =  -1");
    println!();
    println!("  ⟨Cl(1,0,0)⟩  e1 · e1");
    for step in &trace.steps {
        if let Some((ri, pos)) = step.fired {
            println!(
                "                ↓  apply rule {}:  e1·e1  →  {}  @ position {}",
                ri, step.after, pos
            );
        }
    }
    println!("  ⟨Cl(1,0,0)⟩  {}   ✓  done", terminal);
    println!();
    println!(
        "  The computation took {} step. The result is {}.",
        trace.steps.len(),
        terminal
    );

    // ── TRACE → N₁ ───────────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE TRACE  →  N₁");
    println!("{}", bar);
    println!();
    println!("  Every step of the computation is recorded as an exact number.");
    println!("  This record is called the trace.");
    println!();
    if let Some(step) = walk.steps().first() {
        println!("  The step that fired:");
        println!(
            "    rule number:  {}  — the only rule in Cl(1,0,0), implicit",
            step.rule_idx
        );
        println!(
            "    position:     {}  — the only possible position, implicit",
            step.start
        );
        println!(
            "    what matched: {}  — two imaginary generators",
            trit_word_display(&step.matched)
        );
        println!();
        println!("  Rule index and start position are both 0 in this algebra.");
        println!("  There is only one rule and one position where it can apply.");
        println!("  They carry no information. Encoding them wastes trits.");
        println!();
        println!("  N₁ encodes only the informative content: the matched types.");
        println!();
        println!("  matched types:  N  N");
        println!("  Trit encoding:  01  01  (2 bits per trit, trit 0 at LSB)");
        println!();
        println!(
            "  packed:   01 01  =  0b{:04b}  =  {}  =  {}",
            n1_val,
            fmt_hex(n1_val, n1_bits),
            n1_val
        );
    }
    println!();
    println!("  ┌──────────────────────────────────────┐");
    println!("  │  N₁  =  {:<29}│", n1_val);
    println!("  │  {:<36}│", "The trace. 2 trits. 4 bits.");
    println!("  └──────────────────────────────────────┘");

    // ── PROOF ─────────────────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE PROOF");
    println!("{}", bar);
    println!();
    println!("  Now we ask: was the computation correct?");
    println!("  \"Correct\" means three things. Each produces a Trit.");
    println!();
    println!("  ── MINIMAL ───────────────────────────────────────────────");
    println!();
    println!("  Was every step necessary?");
    println!("  The trace has 1 step.");
    println!("  Removing it gives us e1·e1 with no reduction applied.");
    println!("  That is not -1. So the step was necessary.");
    println!();
    println!("  Answer: yes  →  P");
    println!();
    println!("  ── COMPLETE ──────────────────────────────────────────────");
    println!();
    println!("  Is the result fully reduced? Can any rule still fire?");
    println!("  The result is -1. It is a plain number, not a geometric term.");
    println!("  No rule in Cl(1,0,0) applies to a plain number.");
    println!("  Nothing more can be done.");
    println!();
    println!("  Answer: yes  →  P");
    println!();
    println!("  ── CONSISTENT ────────────────────────────────────────────");
    println!();
    println!("  Was there only one possible path to this result?");
    println!("  Only one rule exists in Cl(1,0,0): e1·e1 → -1.");
    println!("  There is no alternative. No two rules could have conflicted.");
    println!();
    println!("  Answer: yes  →  P");
    println!();
    println!("  ── THE THREE TRITS ───────────────────────────────────────");
    println!();
    println!("  minimal    =  P  =  10");
    println!("  complete   =  P  =  10");
    println!("  consistent =  P  =  10");
    println!();
    println!("  Together these form a 3-trit word:  [P · P · P]");
    println!();
    let cert_word = {
        use neberu::gen::Gen;
        use neberu::trit::P;
        Word::from_gens(&[
            Gen { sig: P, idx: 0 },
            Gen { sig: P, idx: 1 },
            Gen { sig: P, idx: 2 },
        ])
    };
    let cert_val = pack_word(&cert_word);
    println!("  Encoded as bits:");
    println!("    10  10  10  =  {:06b}  =  {}", cert_val, cert_val);
    println!();
    println!("  This word is the certificate.");
    println!(
        "  {} is what a correct computation looks like, in this system.",
        cert_val
    );

    // ── KOT GOVERNANCE → N₂ ──────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE KOT GOVERNANCE  →  N₂");
    println!("{}", bar);
    println!();
    println!("  To verify the certificate, we need a standard.");
    println!("  The standard defines what an optimal certificate looks like.");
    println!();
    println!("  The KOT governance has exactly one rule:");
    println!();
    println!("    [P · P · P]  →  P       the only passing certificate");
    println!();
    println!("  Everything else fails. This is not a special assumption —");
    println!("  it is how governance works: no matching rule means no rewrite.");
    println!("  A certificate that does not equal [P·P·P] simply fails to");
    println!("  match the one rule that produces a positive verdict.");
    println!();
    println!("  Enumerating 7 failure cases is unnecessary. The closed-world");
    println!("  assumption handles them. N₂ encodes one complete rule:");
    println!();
    println!(
        "  source  [P·P·P]:   10  10  10  =  0b{:06b}  =  {}   (6 bits)",
        passing_cert, passing_cert
    );
    println!(
        "  verdict      P :         10  =  0b{:02b}  =  {}       (2 bits)",
        TRIT_P.bits(),
        TRIT_P.bits()
    );
    println!();
    println!(
        "  concatenated:  10 10 10 10  =  0b{:08b}  =  {}  =  {}",
        n2_val,
        fmt_hex(n2_val, n2_bits),
        n2_val
    );
    println!();
    println!("  Note: N₂ = [P·P·P·P] — four P-trits. The passing rule is");
    println!("  itself all-positive. The axiom is self-similar.");
    println!();
    println!("  ┌──────────────────────────────────────┐");
    println!("  │  N₂  =  {:<29}│", n2_val);
    println!("  │  {:<36}│", "The KOT rule. 4 trits. 8 bits.");
    println!("  └──────────────────────────────────────┘");

    // ── CERTIFICATE → N₃ ─────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE CERTIFICATE  →  N₃");
    println!("{}", bar);
    println!();
    println!("  The certificate is the trace joined to the governance.");
    println!("  N₁ is what happened. N₂ is the standard.");
    println!("  Together they are the complete proof.");
    println!();
    println!(
        "  N₁  =  {}  =  {}  ({} bits)",
        n1_val,
        fmt_hex(n1_val, n1_bits),
        n1_bits
    );
    println!(
        "  N₂  =  {}  =  {}  ({} bits)",
        n2_val,
        fmt_hex(n2_val, n2_bits),
        n2_bits
    );
    println!();
    println!(
        "  N₃  =  N₁ | (N₂ << {})  =  {} | {}  =  {}",
        n1_bits,
        n1_val,
        n2_val << n1_bits,
        n3_val
    );
    println!();
    println!("  As a 12-bit trit sequence (trit 0 at LSB):");
    println!("    01  01  |  10  10  10  10");
    println!("    N   N   |  P   P   P   P");
    println!("    ─────   |  ─────────────");
    println!("     N₁     |       N₂");
    println!("  [matched] | [source → verdict]");
    println!();
    println!("  The complete proof in 6 trits:");
    println!("  [N·N] = what was matched  •  [P·P·P] = the passing pattern  •  [P] = verdict");
    println!();
    println!("  In bytes (little-endian):");
    print!("    ");
    for b in &n3_bytes {
        print!("{:02X}  ", b);
    }
    println!();
    println!();
    println!("  ┌──────────────────────────────────────┐");
    println!("  │  N₃  =  {:<29}│", n3_val);
    println!("  │  {:<36}│", "The certificate. 6 trits. 2 bytes.");
    println!("  └──────────────────────────────────────┘");

    // ── VERDICT → Q₁ ─────────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE VERDICT  →  Q₁");
    println!("{}", bar);
    println!();
    println!("  Apply the KOT governance to the certificate word [P·P·P].");
    println!();
    println!("  ⟨KOT⟩  [P · P · P]  =  {}", cert_val);
    println!("          ↓  rule fires:  {}  →  +1", cert_val);
    println!("  ⟨KOT⟩  +1");
    println!();
    println!("  The sign of +1  is  P  =  10  =  {}.", q1_bits);
    println!();
    println!("  ┌──────────────────────────────────────┐");
    println!("  │  Q₁  =  {:<29}│", q_chain);
    println!("  │  {:<36}│", "The computation was optimal.");
    println!("  └──────────────────────────────────────┘");

    // ── SELF-CERTIFICATION → Q₂ ──────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE SELF-CERTIFICATION  →  Q₂");
    println!("{}", bar);
    println!();
    println!("  Now we ask: is the proof itself correct?");
    println!("  We apply the same KOT governance to Q₁'s certificate.");
    println!();
    println!("  Q₁ passed with certificate [P·P·P] = {}.", cert_val);
    println!("  That certificate is itself a value in this system.");
    println!("  We can check it the same way we checked the computation.");
    println!();
    println!("  ⟨KOT⟩  [P · P · P]  =  {}", cert_val);
    println!("          ↓  rule fires:  {}  →  +1", cert_val);
    println!("  ⟨KOT⟩  +1");
    println!();
    println!("  The sign of +1  is  P  =  10  =  {}.", q2_bits);
    println!();
    println!("  ┌────────────────────────────────────────────────────────┐");
    println!("  │  Q₂  =  {:<45}│", q_chain);
    println!(
        "  │  {:<52}│",
        "The proof of the computation is itself correct."
    );
    println!("  └────────────────────────────────────────────────────────┘");

    // ── FIXED POINT ───────────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("THE FIXED POINT");
    println!("{}", bar);
    println!();
    println!("  Q₁  =  {}", q_chain);
    println!("  Q₂  =  {}", q_chain);
    println!();
    println!("  Checking the proof again would give P.");
    println!("  And again. And again. It does not change.");
    println!();
    println!("  This is the fixed point:  P · P  =  P");
    println!("  The axiom applied to itself gives itself.");
    println!("  The system is stable under self-inspection.");

    // ── ALL FIVE NUMBERS ──────────────────────────────────────
    println!();
    println!("{}", bar);
    println!("ALL FIVE NUMBERS");
    println!("{}", bar);
    println!();
    println!("  N₁  =  {:>24}   the trace", n1_val);
    println!("  N₂  =  {:>24}   the standard", n2_val);
    println!("  N₃  =  {:>24}   the certificate", n3_val);
    println!("  Q₁  =  {}   the verdict", q_chain);
    println!("  Q₂  =  {}   the verification", q_chain);
    println!();
    println!("  The proof that  e1·e1 = -1  was computed correctly");
    println!("  is the number  {}.", n3_val);
    println!("  It fits in 2 bytes.");
    println!("  The verdict is {}.", q_chain);
}