nx_sketch_types.nx source
↩ module page · 91 lines · 3610 B
1// sketch_types.nx -- L-4 typed envelope scaffold for streaming sketches.
2//
3// Lossless-language discipline (per nishi-engine docs 17 + 20):
4// every approximate primitive in the substrate ships its loss
5// NAMED, BOUNDED, and QUERYABLE. Sketches are bounded-loss, not
6// silent-loss.
7//
8// Mirrors nishi-engine/packages/core-sketch/src/types.ts so the
9// TS reference layer and the NishiLang sovereign sibling carry
10// identical envelope semantics.
11//
12// All-i64 representation (no f64 source-level types) for C-anchor
13// compatibility. Floating-point quantities (stddev, eps, conf)
14// are stored as fixed-point integers scaled by 10^9 (parts-per-
15// billion). Caller decodes by dividing by 1_000_000_000.
16// stddev_rel = 0.0162 -> stddev_rel_ppb = 16_200_000
17// conf = 0.6827 -> conf_ppb = 682_700_000
18
19// nx_safety_envelope:
20// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
21// sil_target: SIL1
22// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
23// verdict: NOT_YET_EVALUATED
24
25import "nx_syscalls.nx"
26const NX_MAGIC_682700000: i64 = 682700000
27const NX_MAGIC_6827: i64 = 6827
28
29// === MaturityClass =================================================
30//
31// Per nishi-engine doc 16 substrate-types-roadmap. Declares whether
32// a primitive is paper-only, software-emulated reference, or native
33// production. Substrate refuses MaturityClass<Theoretical> in
34// production-graded compositions.
35
36const NX_MATURITY_THEORETICAL: i64 = 0
37const NX_MATURITY_REFERENCE_IMPL: i64 = 1
38const NX_MATURITY_PRODUCTION: i64 = 2
39
40// === AdversarialSafety =============================================
41//
42// Per core-sketch/types.ts. Honest-only sketches MUST NOT be
43// substituted for Adversarial-required sketches at composition
44// boundaries.
45
46const NX_ADV_HONEST: i64 = 0
47const NX_ADV_ADVERSARIAL: i64 = 1
48
49// === ErrorEnvelope kinds ===========================================
50//
51// Discriminator for the envelope's parameter interpretation:
52// ENV_REL_STDDEV: cardinality / mean -- error as fraction of value
53// (stddev / value). param_a holds stddev_rel * 1e9.
54// ENV_RANK_ERROR: quantile / rank-class -- error as fraction of N
55// (max |rank_est - rank_true| / N). param_a holds
56// eps * 1e9.
57// ENV_ABS: frequency / count -- error in same units as value.
58// param_a holds the additive bound.
59
60const NX_ENV_REL_STDDEV: i64 = 0
61const NX_ENV_RANK_ERROR: i64 = 1
62const NX_ENV_ABS: i64 = 2
63
64// === Approximate<i64> typed envelope ==============================
65//
66// 48 bytes = 6 i64 fields. Carries a value + its declared loss.
67// Every sketch's query() function returns this struct (allocated
68// by callee via sys_mmap, owned by caller).
69
70struct ApproxI64 {
71 value: i64,
72 envelope_kind: i64, // 0=rel_stddev, 1=rank_error, 2=abs
73 param_a: i64, // primary envelope parameter (× 1e9 if fp)
74 conf_ppb: i64, // confidence × 1e9 (e.g., NX_MAGIC_682700000 = 0.NX_MAGIC_6827)
75 maturity: i64, // NX_MATURITY_*
76 adv_safety: i64, // NX_ADV_*
77}
78
79// Allocate + populate.
80func nx_approx_new(value: i64, envelope_kind: i64, param_a: i64,
81 conf_ppb: i64, maturity: i64, adv_safety: i64) -> *ApproxI64 {
82 let raw: *u8 = sys_mmap(48)
83 let a: *ApproxI64 = raw as *ApproxI64
84 a.value = value
85 a.envelope_kind = envelope_kind
86 a.param_a = param_a
87 a.conf_ppb = conf_ppb
88 a.maturity = maturity
89 a.adv_safety = adv_safety
90 return a
91}