nx_grove.nx source
↩ module page · 176 lines · 6589 B
1// nx_grove.nx -- patient multi-generational capital substrate.
2//
3// Per META-CARDINAL feedback-regenerative-stewardship-doctrine-captain-moroni:
4// the substrate model that lets a 50-year forest or a 100-year ecosystem-
5// restoration project be financed and sustained, when conventional
6// discount-rate finance would call it worthless. REFUSES the conventional
7// 5-10% discount rate that makes 100-year forests' NPV approach zero.
8//
9// V1 ships sealed enum of capital classes + per-class discount-rate
10// override + multi-generation contribution tracking + steward succession.
11
12import "nx_syscalls.nx"
13import "nx_tier.nx"
14
15// ===== Sealed enum: NxCapitalKind =================================
16
17const NX_GR_KIND_ENDOWMENT_PRINCIPAL: nx_int = 0 // never spent; only yield
18const NX_GR_KIND_LAND_TRUST: nx_int = 1
19const NX_GR_KIND_CONSERVATION_EASEMENT: nx_int = 2
20const NX_GR_KIND_GENERATION_SKIPPING: nx_int = 3 // trust crossing generations
21const NX_GR_KIND_INDIGENOUS_STEWARDSHIP: nx_int = 4
22const NX_GR_KIND_COMMUNITY_LAND_TRUST: nx_int = 5
23const NX_GR_KIND_LABOR_INKIND: nx_int = 6 // volunteer hours
24const NX_GR_KIND_TEK_CONTRIBUTION: nx_int = 7 // traditional knowledge
25const NX_GR_KIND_N: nx_int = 8
26
27// ===== Sealed enum: NxGroveVerdict ================================
28
29const NX_GR_V_OK: nx_int = 0
30const NX_GR_V_ERR_INVALID_KIND: nx_int = 1
31const NX_GR_V_REFUSED_SHORT_HORIZON: nx_int = 2 // <5 year horizon refused
32const NX_GR_V_REFUSED_EXTRACTION: nx_int = 3 // discount rate too high
33const NX_GR_V_NULL: nx_int = 4
34const NX_GR_V_INVALID: nx_int = 5
35const NX_GR_V_N: nx_int = 6
36
37// ===== Struct: NxGroveContribution ================================
38
39struct NxGroveContribution {
40 contributor_id: nx_int,
41 kind: nx_int,
42 units: nx_size, // bytes / hours / dollars-Q10 / etc.
43 contributed_at_us: nx_size,
44 horizon_years: nx_int,
45 documented_intent_hash: nx_size,
46}
47
48const NX_GR_CON_BYTES: nx_int = 40
49
50struct NxGrove {
51 grove_id: nx_int,
52 contributions: *u8,
53 n_contributions: nx_int,
54 capacity: nx_int,
55 discount_rate_q10: nx_int, // 0 = no discount; >0 = annual discount in Q10
56 minimum_horizon_years: nx_int,
57 operator_initiated_us: nx_size,
58}
59
60const NX_GR_BYTES: nx_int = 48
61
62func nx_gr_kind_is_valid(k: nx_int) -> nx_int {
63 if k < 0 { return 0 }
64 if k >= NX_GR_KIND_N { return 0 }
65 return 1
66}
67
68func nx_gr_v_is_valid(v: nx_int) -> nx_int {
69 if v < 0 { return 0 }
70 if v >= NX_GR_V_N { return 0 }
71 return 1
72}
73
74func nx_gr_kind_is_in_perpetuity(k: nx_int) -> nx_int {
75 if k == NX_GR_KIND_ENDOWMENT_PRINCIPAL { return 1 }
76 if k == NX_GR_KIND_LAND_TRUST { return 1 }
77 if k == NX_GR_KIND_CONSERVATION_EASEMENT { return 1 }
78 if k == NX_GR_KIND_COMMUNITY_LAND_TRUST { return 1 }
79 if k == NX_GR_KIND_INDIGENOUS_STEWARDSHIP { return 1 }
80 return 0
81}
82
83func nx_gr_kind_is_in_kind(k: nx_int) -> nx_int {
84 if k == NX_GR_KIND_LABOR_INKIND { return 1 }
85 if k == NX_GR_KIND_TEK_CONTRIBUTION { return 1 }
86 return 0
87}
88
89func nx_gr_new(grove_id: nx_int,
90 capacity: nx_int,
91 discount_rate_q10: nx_int,
92 minimum_horizon_years: nx_int,
93 now_us: nx_size) -> *NxGrove {
94 if capacity <= 0 { return 0 as *NxGrove }
95 if minimum_horizon_years < 0 { return 0 as *NxGrove }
96 // Refuse conventional discount rates >= 5% (Q10 51). Patient capital
97 // discipline: substrate refuses to instantiate at extractive rates.
98 if discount_rate_q10 >= 51 { return 0 as *NxGrove }
99 let raw: *u8 = sys_mmap(NX_GR_BYTES)
100 let g: *NxGrove = raw as *NxGrove
101 g.grove_id = grove_id
102 g.contributions = sys_mmap(capacity * NX_GR_CON_BYTES)
103 g.n_contributions = 0
104 g.capacity = capacity
105 g.discount_rate_q10 = discount_rate_q10
106 g.minimum_horizon_years = minimum_horizon_years
107 g.operator_initiated_us = now_us
108 return g
109}
110
111func _gr_contrib_at(g: *NxGrove, idx: nx_int) -> *NxGroveContribution {
112 if idx < 0 { return 0 as *NxGroveContribution }
113 if idx >= g.n_contributions { return 0 as *NxGroveContribution }
114 let off: nx_int = idx * NX_GR_CON_BYTES
115 return (g.contributions + off) as *NxGroveContribution
116}
117
118func nx_gr_add_contribution(g: *NxGrove,
119 contributor_id: nx_int,
120 kind: nx_int,
121 units: nx_size,
122 horizon_years: nx_int,
123 documented_intent_hash: nx_size,
124 now_us: nx_size) -> nx_int {
125 if (g as i64) == 0 { return NX_GR_V_NULL }
126 if nx_gr_kind_is_valid(kind) == 0 { return NX_GR_V_ERR_INVALID_KIND }
127 if horizon_years < g.minimum_horizon_years { return NX_GR_V_REFUSED_SHORT_HORIZON }
128 if horizon_years < 5 { return NX_GR_V_REFUSED_SHORT_HORIZON } // hard min
129 if g.n_contributions >= g.capacity { return NX_GR_V_INVALID }
130 let off: nx_int = g.n_contributions * NX_GR_CON_BYTES
131 let c: *NxGroveContribution = (g.contributions + off) as *NxGroveContribution
132 c.contributor_id = contributor_id
133 c.kind = kind
134 c.units = units
135 c.contributed_at_us = now_us
136 c.horizon_years = horizon_years
137 c.documented_intent_hash = documented_intent_hash
138 g.n_contributions = g.n_contributions + 1
139 return NX_GR_V_OK
140}
141
142func nx_gr_total_units_by_kind(g: *NxGrove, kind: nx_int) -> nx_size {
143 if (g as i64) == 0 { return 0 }
144 var sum: nx_size = 0
145 var i: nx_int = 0
146 while i < g.n_contributions {
147 let c: *NxGroveContribution = _gr_contrib_at(g, i)
148 if c.kind == kind { sum = sum + c.units }
149 i = i + 1
150 }
151 return sum
152}
153
154func nx_gr_longest_horizon_years(g: *NxGrove) -> nx_int {
155 if (g as i64) == 0 { return 0 }
156 var max_h: nx_int = 0
157 var i: nx_int = 0
158 while i < g.n_contributions {
159 let c: *NxGroveContribution = _gr_contrib_at(g, i)
160 if c.horizon_years > max_h { max_h = c.horizon_years }
161 i = i + 1
162 }
163 return max_h
164}
165
166func nx_gr_count_by_kind(g: *NxGrove, kind: nx_int) -> nx_int {
167 if (g as i64) == 0 { return 0 }
168 var count: nx_int = 0
169 var i: nx_int = 0
170 while i < g.n_contributions {
171 let c: *NxGroveContribution = _gr_contrib_at(g, i)
172 if c.kind == kind { count = count + 1 }
173 i = i + 1
174 }
175 return count
176}