nx_tier_swap_demo.nx source
↩ module page · 62 lines · 1951 B
1// nx_tier_swap_demo.nx -- proof that nx_tier.nx swap is real.
2//
3// Computes the triangular sum 1 + 2 + ... + N where N is chosen
4// to overflow signed 32-bit int but fit signed 64-bit.
5//
6// sum(N) = N * (N+1) / 2
7// N = 100000 -> sum = 5,000,050,000 (~5e9)
8// int32 max = 2,147,483,647 (~2.1e9) -> overflows
9// int64 max = 9.2e18 -> fits
10//
11// With nx_int = i64: prints "5000050000" and exits 0
12// With nx_int = i32: would wrap to a negative number and exit 1
13// (after editing nx_tier.nx to swap the alias).
14//
15// Substrate-level proof that one edit to nx_tier.nx propagates.
16
17// nx_safety_envelope:
18// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
19// sil_target: SIL1
20// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
21// verdict: NOT_YET_EVALUATED
22
23import "nx_syscalls.nx"
24import "nx_runtime.nx"
25import "nx_tier.nx"
26const K_MAGIC_100000: i64 = 100000
27const K_MAGIC_5000050000: i64 = 5000050000
28
29const STDOUT: i64 = 1
30
31func triangular_sum(n: nx_int) -> nx_int {
32 var sum: nx_int = 0
33 var i: nx_int = 1
34 while i <= n {
35 sum = sum + i
36 i = i + 1
37 }
38 return sum
39}
40
41func main() -> i64 {
42 let n: nx_int = K_MAGIC_100000
43 let s: nx_int = triangular_sum(n)
44
45 let label: *u8 = "triangular_sum(100000) with nx_int = " as *u8
46 sys_write(STDOUT, label, strlen(label))
47
48 // Print the type's effective width by emitting whether the
49 // computation overflowed. Expected with i64: 5000050000.
50 print_i64(s)
51 let nl: *u8 = "\n" as *u8
52 sys_write(STDOUT, nl, 1)
53
54 if s == K_MAGIC_5000050000 {
55 let pass: *u8 = "PASS: nx_int holds N>=i32-overflow (>= i64)\n" as *u8
56 sys_write(STDOUT, pass, strlen(pass))
57 return 0
58 }
59 let fail: *u8 = "FAIL/EXPECTED: nx_int wrapped (likely i32-aliased)\n" as *u8
60 sys_write(STDOUT, fail, strlen(fail))
61 return 1
62}