rei-lang 0.3.0 → 0.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 (40) hide show
  1. package/LICENSE +230 -191
  2. package/PEACE_USE_CLAUSE.md +95 -0
  3. package/README.md +257 -186
  4. package/bin/rei.js +259 -144
  5. package/dist/index.d.mts +257 -0
  6. package/dist/index.d.ts +181 -93
  7. package/dist/index.js +5710 -239
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +8741 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/package.json +73 -62
  12. package/spec/REI_BNF_v0.2.md +398 -0
  13. package/spec/REI_BNF_v0.3.md +358 -0
  14. package/spec/REI_SPEC_v0.1.md +587 -0
  15. package/theory/LICENSE-THEORY.md +57 -0
  16. package/theory/README.md +85 -0
  17. package/theory/category-c-design.md +502 -0
  18. package/theory/core-theories-11-14.md +799 -0
  19. package/theory/core-theories-15-21.md +675 -0
  20. package/theory/core-theories-8-10.md +582 -0
  21. package/theory/d-fumt-overview.md +99 -0
  22. package/theory/genesis-axiom-system.md +124 -0
  23. package/theory/gft-theory.md +160 -0
  24. package/theory/index.ts +9 -0
  25. package/theory/notation-equivalence.md +134 -0
  26. package/theory/semantic-compressor.ts +903 -0
  27. package/theory/stdlib-tier1-design.md +477 -0
  28. package/theory/theories-11-14.ts +466 -0
  29. package/theory/theories-15-21.ts +762 -0
  30. package/theory/theories-22-28.ts +794 -0
  31. package/theory/theories-29-35.ts +607 -0
  32. package/theory/theories-36-42.ts +937 -0
  33. package/theory/theories-43-49.ts +1298 -0
  34. package/theory/theories-50-56.ts +1449 -0
  35. package/theory/theories-57-66.ts +1724 -0
  36. package/theory/theories-67.ts +1696 -0
  37. package/theory/theories-8-10.ts +284 -0
  38. package/dist/index.cjs +0 -3282
  39. package/dist/index.cjs.map +0 -1
  40. package/dist/index.d.cts +0 -169
package/README.md CHANGED
@@ -1,186 +1,257 @@
1
- # Rei (0₀式/れいしき) — D-FUMT Computational Language
2
-
3
- [![npm version](https://img.shields.io/npm/v/rei-lang)](https://www.npmjs.com/package/rei-lang)
4
- [![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE)
5
- [![Tests](https://img.shields.io/badge/tests-226%2F226-brightgreen)]()
6
-
7
- **Rei** is a programming language based on D-FUMT (Dimensional Fujimoto Universal Mathematical Theory) a paradigm where every computation flows through the center-periphery pattern `𝕄{center; periphery}`.
8
-
9
- v0.3 introduces the **Space-Layer-Diffusion** computation model and **Category C** — 25 consciousness axioms that give values self-awareness, tendency, resonance, and multi-perspective computation.
10
-
11
- ## Install
12
-
13
- ```bash
14
- npm install rei-lang
15
- ```
16
-
17
- ## Quick Start
18
-
19
- ```typescript
20
- import { rei } from 'rei-lang';
21
-
22
- // Multi-dimensional computation — center-periphery pattern
23
- rei('𝕄{5; 1, 2, 3, 4} |> compute'); // 7.5
24
-
25
- // σ: Self-reference — every value knows itself (C1)
26
- rei('42 |> sigma'); // { field, will, memory, ... }
27
-
28
- // Project anything into 𝕄 (N1)
29
- rei('[1, 5, 3] |> project("max") |> compute'); // 5 + (1+3)/2 = 7
30
-
31
- // Multi-mode consensus (A3)
32
- rei('𝕄{5; 1, 2, 3, 4} |> consensus'); // median, mean, agreement
33
- ```
34
-
35
- ## CLI
36
-
37
- ```bash
38
- # REPL
39
- npx rei
40
-
41
- # Evaluate expression
42
- npx rei -e '𝕄{5; 1, 2, 3} |> compute'
43
-
44
- # Execute file
45
- npx rei script.rei
46
- ```
47
-
48
- ## v0.3 — Category C: 25 Consciousness Axioms
49
-
50
- Every value in Rei has σ (self-reference), τ (tendency), and participates in an interconnected computation field inspired by Buddhist philosophy — particularly *Indra's Net* (因陀羅網) where every jewel reflects all others.
51
-
52
- ### Axiom Groups
53
-
54
- | Group | Axioms | Philosophy | Commands |
55
- |-------|--------|-----------|----------|
56
- | **C** Consciousness | C1-C5 | σ全値型, τ傾向性, 応答, 覚醒, 共鳴 | `sigma`, `awareness`, `resonate` |
57
- | **N** Non-Numerical | N1-N5 | 射影, 複数射影, 型変換, 合成, 完全性 | `project`, `project_as`, `encode` |
58
- | **M** Meta-Math | M1-M5 | 計算多元性, 等価, 合成, 導出, 空間 | `compute :mode`, `derive_mode`, `mode_space` |
59
- | **U** Universal | U1-U5 | 構造還元, 変換保存, 再帰, 架橋, 完全性 | `project_all`, `transform`, `bridge` |
60
- | **A** Alternative | A1-A5 | 解の多元性, 変換, 合意, 評価, 完全性 | `compute_all`, `consensus`, `best`, `rank` |
61
-
62
- ### Examples
63
-
64
- ```typescript
65
- import { rei } from 'rei-lang';
66
-
67
- // ── C1: σ — Self-reference for all types ──
68
- rei('42 |> sigma'); // SigmaResult with field, will, memory
69
- rei('"hello" |> sigma'); // String σ
70
- rei('𝕄{5; 1, 2, 3} |> sigma'); // MDim σ
71
-
72
- // ── C2: τ — Tendency tracking through pipes ──
73
- rei('100 |> sqrt |> sqrt |> sigma'); // will.tendency = "contract"
74
-
75
- // ── C4: Awakening — Values gain awareness through computation ──
76
- rei('42 |> awareness'); // Low (< 0.5)
77
- rei('𝕄{5; 1,2,3,4,5,6,7,8} |> abs |> negate |> abs |> awakened'); // true
78
-
79
- // ── C5: Resonance — Non-local connection between values ──
80
- rei('𝕄{5; 1, 2, 3} |> resonate(𝕄{5; 1, 2, 3})'); // strength > 0.8
81
- rei('𝕄{5; 4, 6, 3} |> resonance_map'); // All-pairs resonance
82
-
83
- // ── N1-N3: Projection — Anything becomes 𝕄 ──
84
- rei('[1, 5, 3] |> project("max")'); // 𝕄{5; 1, 3}
85
- rei('𝕄{5; 1, 2, 3} |> project_as("graph")'); // GraphProjection
86
- rei('𝕄{5; 1, 2, 3} |> project_as("tree")'); // TreeProjection
87
-
88
- // ── M1: Multiple computation modes ──
89
- rei('𝕄{5; 1, 2, 3, 4} |> compute :weighted'); // 7.5
90
- rei('𝕄{5; 1, 2, 3, 4} |> compute :geometric'); // geometric mean
91
- rei('𝕄{5; 1, 2, 3, 4} |> compute :entropy'); // information entropy
92
-
93
- // ── M4: Mode derivation Create new modes from existing ──
94
- rei('𝕄{5; 1, 2, 3, 4} |> derive_mode("weighted", 0.7, "geometric", 0.3)');
95
-
96
- // ── U3: Hierarchical recursion — 𝕄 within 𝕄 ──
97
- rei('𝕄{5; 1, 2, 3} |> nest(1) |> depth'); // 1
98
-
99
- // ── U4: Domain bridging — Structure transfer ──
100
- rei('𝕄{5; 1, 2, 3} |> bridge(𝕄{10; 2, 4, 6})'); // scaleFactor: 2
101
-
102
- // ── U5: Completeness — Encode/Decode any value ──
103
- rei('[1, 2, 3] |> encode'); // 𝕄{1; 2, 3}
104
- rei('𝕄{5; 1, 2, 3} |> decode("array")'); // [5, 1, 2, 3]
105
- rei('"hi" |> encode'); // 𝕄{104; 105}
106
-
107
- // ── A1-A3: Multiple solutions & consensus ──
108
- rei('𝕄{5; 1, 2, 3, 4} |> compute_all'); // 8 solutions (all modes)
109
- rei('𝕄{5; 1, 2, 3, 4} |> consensus'); // { median, mean, agreement }
110
-
111
- // ── A4: Solution evaluation ──
112
- rei('𝕄{5; 1, 2, 3, 4} |> best("max")'); // Highest-value solution
113
- rei('𝕄{5; 1, 2, 3, 4} |> rank'); // All solutions ranked
114
-
115
- // ── Space-Layer-Diffusion ──
116
- rei('let s = space { layer 0: 𝕄{5; 1, 2, 3} }; s |> sigma');
117
- ```
118
-
119
- ## 8 Computation Modes
120
-
121
- | Mode | Formula | Meaning |
122
- |------|---------|---------|
123
- | `weighted` | `center + mean(neighbors)` | Balanced perspective (default) |
124
- | `geometric` | `center × geo_mean(neighbors)` | Multiplicative relationships |
125
- | `harmonic` | `center + harmonic_mean(neighbors)` | Rate/ratio problems |
126
- | `median` | `center + median(neighbors)` | Robust to outliers |
127
- | `entropy` | `center × (1 + H(neighbors))` | Information content |
128
- | `minkowski` | `center + minkowski_dist(neighbors)` | Spatial distance |
129
- | `multiplicative` | `center × Π(neighbors)` | Product relationships |
130
- | `exponential` | `center × e^(mean(neighbors))` | Growth patterns |
131
-
132
- ## API
133
-
134
- ```typescript
135
- import { rei, Lexer, Parser, Evaluator } from 'rei-lang';
136
-
137
- // High-level API
138
- rei('𝕄{5; 1, 2, 3} |> compute'); // Evaluate code
139
- rei.parse('𝕄{5; 1, 2, 3}'); // Get AST
140
- rei.tokenize('42 |> sigma'); // Get token stream
141
- rei.reset(); // Reset evaluator state
142
-
143
- // Low-level API
144
- const tokens = new Lexer(code).tokenize();
145
- const ast = new Parser(tokens).parseProgram();
146
- const ev = new Evaluator();
147
- const result = ev.eval(ast);
148
- ```
149
-
150
- ## Benchmarks
151
-
152
- Average **74% code reduction** compared to equivalent Python/JavaScript:
153
-
154
- | Task | Python/JS | Rei | Reduction |
155
- |------|-----------|-----|-----------|
156
- | Image kernel | 32 lines | 8 lines | 4× |
157
- | Multi-dim aggregation | 45 lines | 12 lines | 3.7× |
158
- | Graph transformation | 52 lines | 14 lines | 3.7× |
159
- | ETL pipeline | 50 lines | 16 lines | 3.1× |
160
-
161
- The center-periphery pattern `𝕄` being a language primitive is the fundamental advantage — what other languages build from loops, maps, and reduces, Rei expresses directly.
162
-
163
- ## Theory
164
-
165
- Rei implements D-FUMT (Dimensional Fujimoto Universal Mathematical Theory) — a framework of 66 mathematical theories spanning pure mathematics to consciousness studies. The 25 Category C axioms formalize how values can:
166
-
167
- - **Know themselves** (C1: σ self-reference)
168
- - **Have tendencies** (C2: τ tracking)
169
- - **Respond to stimuli** (C3: response)
170
- - **Awaken through computation** (C4: awareness)
171
- - **Resonate non-locally** (C5: Indra's Net)
172
-
173
- For the full theoretical framework, see the [D-FUMT documentation on note.com](https://note.com/fc0web).
174
-
175
- ## Version History
176
-
177
- | Version | Tests | Highlights |
178
- |---------|-------|-----------|
179
- | v0.1.0 | 85 | Core language: 𝕄, pipes, 0-extensions, Genesis |
180
- | v0.2.0 | 85 | Tetravalent logic, compression operators |
181
- | v0.2.1 | 91 | License fix, build optimization |
182
- | **v0.3.0** | **226** | **Space-Layer-Diffusion, Category C (25 axioms), Tier 1-5** |
183
-
184
- ## License
185
-
186
- Apache-2.0 © Nobuki Fujimoto
1
+ # Rei (0₀式) — D-FUMT Computational Language
2
+
3
+ [![npm version](https://img.shields.io/npm/v/rei-lang)](https://www.npmjs.com/package/rei-lang)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-gold.svg)](./LICENSE)
5
+ [![Tests](https://img.shields.io/badge/tests-85%2F85-brightgreen)]()
6
+
7
+ **Rei** (0₀式 / れいしき) is a mathematical computation language based on **D-FUMT** (Dimensional Fujimoto Universal Mathematical Theory). Its center-periphery patterns as language primitives achieve an **average 74% code reduction** over equivalent implementations in general-purpose languages.
8
+
9
+ **Author:** Nobuki Fujimoto
10
+
11
+ ---
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm install rei-lang
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ### As a Library
22
+
23
+ ```typescript
24
+ import { rei } from 'rei-lang';
25
+
26
+ // Multi-dimensional number computation
27
+ rei('let field = 𝕄{5; 1, 2, 3, 4}');
28
+ const result = rei('field |> compute :weighted');
29
+ console.log(result); // 7.5
30
+
31
+ // Define functions with compress
32
+ rei('compress energy(m) = m |> compute :weighted');
33
+ rei('let e = energy(𝕄{0; 10, 20, 30})');
34
+ console.log(rei('e')); // 20
35
+
36
+ // Genesis axiom system
37
+ rei('let g = genesis()');
38
+ rei('g |> forward');
39
+ rei('g |> forward');
40
+ console.log(rei('g.state')); // "line"
41
+
42
+ // Reset state between sessions
43
+ rei.reset();
44
+ ```
45
+
46
+ ### Interactive REPL
47
+
48
+ ```bash
49
+ npx rei
50
+ ```
51
+
52
+ ```
53
+ ╔══════════════════════════════════════════╗
54
+ ║ Rei (0₀式) REPL v0.2.0 ║
55
+ ║ D-FUMT Computational Language ║
56
+ ╚══════════════════════════════════════════╝
57
+
58
+ > 𝕄{5; 1, 2, 3, 4} |> compute :weighted
59
+ 7.5
60
+
61
+ 零 > compress karma(i, e, r) = i * e * r
62
+ compress karma(i, e, r)
63
+
64
+ 零 > karma(0.8, 0.9, 0.7)
65
+ 0.504
66
+ ```
67
+
68
+ ### Execute a File
69
+
70
+ ```bash
71
+ npx rei program.rei
72
+ ```
73
+
74
+ ### Inline Evaluation
75
+
76
+ ```bash
77
+ npx rei -e "2 + 3 * 4"
78
+ # → 14
79
+ ```
80
+
81
+ ---
82
+
83
+ ## Language Features
84
+
85
+ ### Multi-Dimensional Numbers (𝕄)
86
+
87
+ The core data structure. A center value with peripheral neighbors, computed in 4 modes:
88
+
89
+ ```rei
90
+ let m = 𝕄{5; 1, 2, 3, 4}
91
+
92
+ m |> compute :weighted // center + weighted avg of neighbors
93
+ m |> compute :multiplicative // center × Π(1 + nᵢ)
94
+ m |> compute :harmonic // center + n / Σ(1/|nᵢ|)
95
+ m |> compute :exponential // center × avg(e^nᵢ)
96
+ ```
97
+
98
+ ### Extended Numbers (拡張数)
99
+
100
+ Numbers with subscript-based dimensional extension:
101
+
102
+ ```rei
103
+ let a = 0ooo // 3rd-order extension of zero
104
+ a >> :x >> :x // extend: order 3 5
105
+ a << // reduce: order 3 → 2
106
+ a |> valStar // numeric projection: 0.001
107
+
108
+ πooo // π extended to 3rd order
109
+ 0₀ // D-FUMT zero symbol
110
+ ```
111
+
112
+ ### Compress (関数定義)
113
+
114
+ Functions are defined with `compress` — reflecting D-FUMT's compression philosophy:
115
+
116
+ ```rei
117
+ compress distance(x, y) = sqrt(x * x + y * y)
118
+ compress field(c, r) = 𝕄{c; r, r, r, r}
119
+
120
+ distance(3, 4) // 5
121
+ field(10, 2) |> compute :weighted // 12
122
+ ```
123
+
124
+ ### Pipe Operator (|>)
125
+
126
+ Center-to-periphery data flow:
127
+
128
+ ```rei
129
+ -25 |> abs |> sqrt // 5
130
+ [3, 1, 2] |> sort |> reverse // [3, 2, 1]
131
+ "hello" |> upper // "HELLO"
132
+ 𝕄{0; 1, 2, 3} |> normalize // normalized neighbors
133
+ ```
134
+
135
+ ### Genesis Axiom System (生成公理系)
136
+
137
+ Models computational emergence from void:
138
+
139
+ ```rei
140
+ let g = genesis() // void
141
+ g |> forward // void dot
142
+ g |> forward // dot → line
143
+ g |> forward // line → surface
144
+ g |> forward // surface → solid
145
+ g |> forward // solid → omega (Ω)
146
+ g.omega // 1
147
+ ```
148
+
149
+ Or from the primordial dot (・):
150
+
151
+ ```rei
152
+ let g =
153
+ g |> forward // dot
154
+ g |> forward // line
155
+ ```
156
+
157
+ ### Four-Valued Logic (四価0π)
158
+
159
+ Beyond true/false Theory #21:
160
+
161
+ ```rei
162
+ ⊤ // true
163
+ ⊥ // false
164
+ ⊤π // true-pi (π-rotated truth)
165
+ ⊥π // false-pi
166
+
167
+ ¬⊤ //
168
+ ⊥ //
169
+ ⊤ //
170
+ ```
171
+
172
+ ### Variable Binding
173
+
174
+ ```rei
175
+ let x = 42 // immutable
176
+ let mut y = 10 // mutable
177
+ ```
178
+
179
+ ---
180
+
181
+ ## Benchmarks
182
+
183
+ | Task | Conventional | Rei | Reduction |
184
+ |------|-------------|-----|-----------|
185
+ | Image kernel calculations | 32 lines | 8 lines | **4×** |
186
+ | Multi-dimensional data aggregation | 45 lines | 12 lines | **3.7×** |
187
+ | Graph structure transformation | 52 lines | 14 lines | **3.7×** |
188
+ | **Average** | | | **74%** |
189
+
190
+ ---
191
+
192
+ ## API Reference
193
+
194
+ ### `rei(code: string): ReiValue`
195
+
196
+ Evaluate a string of Rei code. State persists across calls.
197
+
198
+ ### `rei.reset(): void`
199
+
200
+ Clear all variable and function bindings.
201
+
202
+ ### `rei.parse(code: string): ASTNode`
203
+
204
+ Parse code and return the AST without evaluating.
205
+
206
+ ### `rei.tokenize(code: string): Token[]`
207
+
208
+ Tokenize code and return the token stream.
209
+
210
+ ### Classes
211
+
212
+ - `Lexer` — Tokenizer
213
+ - `Parser` — Recursive descent parser
214
+ - `Evaluator` — AST evaluator with environment/scope chain
215
+ - `Environment` — Scope management
216
+
217
+ ### Types
218
+
219
+ - `MultiDimNumber` — `{ center, neighbors, mode, weights? }`
220
+ - `ReiExtended` — `{ base, order, subscripts, valStar() }`
221
+ - `GenesisState` — `{ state, omega, history }`
222
+ - `ReiFunction` — `{ name, params, body, closure }`
223
+ - `Quad` — `{ value: 'top' | 'bottom' | 'topPi' | 'bottomPi' }`
224
+
225
+ ---
226
+
227
+ ## BNF Specification
228
+
229
+ The complete BNF v0.2 specification is available in the repository.
230
+
231
+ Key features integrated from 21 D-FUMT theories:
232
+ - 45 keywords, 10 operators, 9 types
233
+ - Full backward compatibility with v0.1
234
+ - Complete operator precedence table
235
+
236
+ ---
237
+
238
+ ## Theoretical Foundation
239
+
240
+ Rei is grounded in **D-FUMT** (Dimensional Fujimoto Universal Mathematical Theory), a framework of 66 interconnected theories spanning pure mathematics to philosophy and AI consciousness. The language's core innovation — **center-periphery patterns as language primitives** — derives from D-FUMT's multi-dimensional number system theory.
241
+
242
+ ---
243
+ ☮️ Peace Use Clause / 平和利用条項
244
+ Rei is licensed under Apache 2.0 with an additional Peace Use Clause.
245
+ Rei は Apache 2.0 ライセンスに加え、平和利用条項が適用されます。
246
+ Rooted in the Buddhist concept of "Kū" (空, Emptiness) and D-FUMT's consciousness mathematics, Rei is designed exclusively for the peaceful advancement of humanity. This software may not be used for:
247
+ 仏教の「空」の概念と D-FUMT の意識数学に基づき、Rei は人類の平和的発展のためにのみ設計されています。以下の目的での使用は禁止されています:
248
+
249
+ 🚫 Weapons, military systems, or LAWS / 兵器・軍事システム・自律型致死兵器
250
+ 🚫 Human rights violations / 人権侵害
251
+ 🚫 Intentional environmental destruction / 意図的な環境破壊
252
+
253
+ ✅ Education, research, humanitarian aid, and creative endeavors are encouraged.
254
+ ✅ 教育・研究・人道支援・創造的活動での使用を推奨します。
255
+ See PEACE_USE_CLAUSE.md for full details.
256
+
257
+ MIT © Nobuki Fujimoto