rei-lang 0.4.0 → 0.5.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.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Rei (0₀式) — D-FUMT Computational Language
2
2
 
3
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)]()
4
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-gold.svg)](./LICENSE)
5
+ [![Tests](https://img.shields.io/badge/tests-721%2F721-brightgreen)]()
6
6
 
7
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
8
 
@@ -10,6 +10,16 @@
10
10
 
11
11
  ---
12
12
 
13
+ ## What's New in v0.5
14
+
15
+ v0.5 introduces a **self-organizing agent runtime** — entities perceive, decide, and act autonomously, coordinated by a conflict-resolving mediator.
16
+
17
+ - **EventBus** — Type-safe event-driven architecture with flow momentum tracking
18
+ - **Entity Agent** — Six-attribute autonomous agents (perceive → decide → act cycle)
19
+ - **Mediator** — Concurrent execution engine with conflict detection and resolution strategies
20
+
21
+ ---
22
+
13
23
  ## Install
14
24
 
15
25
  ```bash
@@ -51,8 +61,8 @@ npx rei
51
61
 
52
62
  ```
53
63
  ╔══════════════════════════════════════════╗
54
- ║ Rei (0₀式) REPL v0.2.0
55
- ║ D-FUMT Computational Language
64
+ ║ Rei (0₀式) REPL v0.5.0
65
+ ║ D-FUMT Computational Language
56
66
  ╚══════════════════════════════════════════╝
57
67
 
58
68
  零 > 𝕄{5; 1, 2, 3, 4} |> compute :weighted
@@ -71,13 +81,6 @@ compress karma(i, e, r)
71
81
  npx rei program.rei
72
82
  ```
73
83
 
74
- ### Inline Evaluation
75
-
76
- ```bash
77
- npx rei -e "2 + 3 * 4"
78
- # → 14
79
- ```
80
-
81
84
  ---
82
85
 
83
86
  ## Language Features
@@ -146,17 +149,9 @@ g |> forward // solid → omega (Ω)
146
149
  g.omega // 1
147
150
  ```
148
151
 
149
- Or from the primordial dot (・):
150
-
151
- ```rei
152
- let g = ・
153
- g |> forward // dot
154
- g |> forward // line
155
- ```
156
-
157
152
  ### Four-Valued Logic (四価0π)
158
153
 
159
- Beyond true/false — Theory #21:
154
+ Beyond true/false — based on D-FUMT Theory #21:
160
155
 
161
156
  ```rei
162
157
  ⊤ // true
@@ -169,11 +164,177 @@ Beyond true/false — Theory #21:
169
164
  ⊥ ∨ ⊤ // ⊤
170
165
  ```
171
166
 
172
- ### Variable Binding
167
+ ---
168
+
169
+ ## v0.5 Agent Runtime
170
+
171
+ ### EventBus (イベントバス)
172
+
173
+ Type-safe event system with flow momentum tracking. Events follow a `category:action` pattern (e.g., `entity:fuse`, `agent:act`, `space:diffuse`).
174
+
175
+ ```typescript
176
+ import { rei, ReiEventBus } from 'rei-lang';
177
+
178
+ // Access via Evaluator
179
+ const ev = rei.evaluator();
180
+ const bus = ev.eventBus;
181
+
182
+ // Subscribe to events
183
+ bus.on('entity:fuse', (event) => {
184
+ console.log('Fusion occurred:', event.data);
185
+ });
186
+
187
+ // Subscribe with filter
188
+ bus.subscribe(
189
+ (e) => e.category === 'agent',
190
+ (e) => console.log(`Agent ${e.data.agentId}: ${e.action}`)
191
+ );
192
+ ```
193
+
194
+ In Rei syntax with pipe commands:
195
+
196
+ ```rei
197
+ // Emit a custom event
198
+ "mySource" |> emit("entity:transform", "data")
199
+
200
+ // Subscribe to events
201
+ "entity:*" |> subscribe
202
+
203
+ // Check flow momentum state
204
+ 0 |> flow_momentum // → { state: "expanding", rate: 12.5, ... }
205
+ ```
206
+
207
+ ### Entity Agent (自律エンティティ)
208
+
209
+ Entities in Rei are autonomous agents with the six D-FUMT attributes (場・流れ・記憶・層・関係・意志), executing a perceive → decide → act lifecycle.
173
210
 
174
211
  ```rei
175
- let x = 42 // immutable
176
- let mut y = 10 // mutable
212
+ // Spawn an agent from a value
213
+ let a = 𝕄{10; 1, 2, 3} |> spawn
214
+
215
+ // Agent lifecycle
216
+ a |> perceive // observe environment → Perception
217
+ a |> decide // choose action → Decision
218
+ a |> act // execute decision → ActionResult
219
+
220
+ // Agent introspection
221
+ a |> agent_sigma // σ metadata: state, step, memory, bindings
222
+ a |> 自律σ // 日本語版
223
+
224
+ // Agent behaviors: reactive / proactive / cooperative / competitive / contemplative
225
+ ```
226
+
227
+ ```typescript
228
+ // Programmatic API
229
+ import { ReiAgent, AgentRegistry } from 'rei-lang';
230
+
231
+ const registry = new AgentRegistry();
232
+ const agent = registry.spawn(42, {
233
+ behavior: 'cooperative',
234
+ autonomyLevel: 0.8,
235
+ });
236
+
237
+ const perception = agent.perceive(registry);
238
+ const decision = agent.decide(perception);
239
+ const result = agent.act(registry, decision);
240
+ ```
241
+
242
+ ### Mediator (調停エンジン)
243
+
244
+ The Mediator coordinates multiple agents running concurrently, detecting and resolving conflicts with configurable strategies.
245
+
246
+ ```rei
247
+ // Concurrent execution — all agents perceive → decide → resolve conflicts → act
248
+ 0 |> mediate // 1 round of concurrent execution
249
+ 0 |> mediate(5) // 5 rounds with convergence detection
250
+ 0 |> mediate(10, 0.8) // max 10 rounds, convergence threshold 0.8
251
+
252
+ // Strategies: priority / cooperative / sequential / cancel_both / mediator
253
+ 0 |> mediate_strategy("cooperative") // 協調 — merge conflicting intentions
254
+ 0 |> 調停戦略("協調") // 日本語版
255
+
256
+ // Inter-agent messaging
257
+ "agent_a" |> mediate_message("agent_b", "hello") // point-to-point
258
+ "agent_a" |> mediate_broadcast("共有データ") // broadcast to all
259
+
260
+ // Mediator σ — statistics and convergence history
261
+ 0 |> mediator_sigma // → { totalRounds, conflicts, convergenceHistory, ... }
262
+ 0 |> 調停σ // 日本語版
263
+ ```
264
+
265
+ Conflict types detected automatically: **target contention** (same target), **resource conflict** (shared resource), **mutual fuse** (A↔B fusion), **contradictory** (fuse vs separate).
266
+
267
+ Resolution strategies:
268
+
269
+ | Strategy | Japanese | Behavior |
270
+ |----------|----------|----------|
271
+ | `priority` | 優先 | Highest confidence × priority wins |
272
+ | `cooperative` | 協調 | Merge intentions into compromise |
273
+ | `sequential` | 順次 | Execute in priority order |
274
+ | `cancel_both` | 両方取消 | Cancel conflicting actions |
275
+ | `mediator` | 調停者 | Mediator's own judgment (中道) |
276
+
277
+ ```typescript
278
+ // Programmatic API
279
+ import { ReiMediator, AgentRegistry } from 'rei-lang';
280
+
281
+ const registry = new AgentRegistry();
282
+ const mediator = new ReiMediator(registry);
283
+
284
+ // Spawn agents
285
+ registry.spawn(10, { behavior: 'cooperative' });
286
+ registry.spawn(20, { behavior: 'competitive' });
287
+
288
+ // Run concurrent rounds with convergence detection
289
+ const result = mediator.run({
290
+ maxRounds: 10,
291
+ convergenceThreshold: 0.8,
292
+ });
293
+ console.log(result.converged, result.totalRounds);
294
+ ```
295
+
296
+ ---
297
+
298
+ ## Six Attributes (六属性)
299
+
300
+ Every entity in Rei carries six attributes from D-FUMT theory:
301
+
302
+ | Attribute | Japanese | Role |
303
+ |-----------|----------|------|
304
+ | Field (場) | ば | Spatial context and neighbors |
305
+ | Flow (流れ) | ながれ | Temporal momentum and EventBus connection |
306
+ | Memory (記憶) | きおく | History of observations and actions |
307
+ | Layer (層) | そう | Hierarchical depth and nesting |
308
+ | Relation (関係) | かんけい | Bindings between entities |
309
+ | Will (意志) | いし | Intention-driven computation strategy |
310
+
311
+ ```rei
312
+ // Relation binding
313
+ let x = 42
314
+ let y = 100
315
+ x |> bind(y, "collaborator") // create a relation
316
+
317
+ // Will-driven computation
318
+ let m = 𝕄{5; 1, 2, 3}
319
+ m |> intend("maximize") // set intention
320
+ m |> will_compute // compute guided by will
321
+ ```
322
+
323
+ ---
324
+
325
+ ## RCT Compression (Rei Compression Theory)
326
+
327
+ D-FUMT Theory #67 — generative compression outperforming gzip on structured data:
328
+
329
+ ```rei
330
+ // Core compression (Direction 1-2)
331
+ let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
332
+ data |> compress // → compact θ representation
333
+ data |> compress |> decompress // → lossless round-trip
334
+
335
+ // Semantic compression (Direction 3)
336
+ "let x = 𝕄{5; 1, 2, 3}" |> semantic_compress // → θ parameters
337
+ "let x = 𝕄{5; 1, 2, 3}" |> 意味圧縮 // 日本語版
177
338
  ```
178
339
 
179
340
  ---
@@ -199,39 +360,66 @@ Evaluate a string of Rei code. State persists across calls.
199
360
 
200
361
  Clear all variable and function bindings.
201
362
 
202
- ### `rei.parse(code: string): ASTNode`
203
-
204
- Parse code and return the AST without evaluating.
363
+ ### `rei.evaluator(): Evaluator`
205
364
 
206
- ### `rei.tokenize(code: string): Token[]`
207
-
208
- Tokenize code and return the token stream.
365
+ Access the underlying Evaluator instance (for EventBus, AgentRegistry, etc.).
209
366
 
210
367
  ### Classes
211
368
 
212
- - `Lexer` Tokenizer
213
- - `Parser` — Recursive descent parser
214
- - `Evaluator` AST evaluator with environment/scope chain
215
- - `Environment` Scope management
369
+ | Class | Description |
370
+ |-------|-------------|
371
+ | `Lexer` | Tokenizer |
372
+ | `Parser` | Recursive descent parser |
373
+ | `Evaluator` | AST evaluator with environment/scope chain |
374
+ | `ReiEventBus` | Type-safe event system with flow momentum |
375
+ | `ReiAgent` | Autonomous entity with six attributes |
376
+ | `AgentRegistry` | Agent lifecycle management |
377
+ | `ReiMediator` | Concurrent execution engine with conflict resolution |
216
378
 
217
379
  ### Types
218
380
 
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' }`
381
+ | Type | Shape |
382
+ |------|-------|
383
+ | `MultiDimNumber` | `{ center, neighbors, mode, weights? }` |
384
+ | `ReiExtended` | `{ base, order, subscripts, valStar() }` |
385
+ | `GenesisState` | `{ state, omega, history }` |
386
+ | `ReiFunction` | `{ name, params, body, closure }` |
387
+ | `Quad` | `{ value: 'top' \| 'bottom' \| 'topPi' \| 'bottomPi' }` |
388
+ | `ReiEvent` | `{ type, category, action, timestamp, data, source?, depth }` |
389
+ | `AgentSigma` | `{ state, kind, behavior, step, perception, decisions, memory }` |
390
+ | `MediatorSigma` | `{ totalRounds, conflicts, convergenceHistory, agentCount }` |
224
391
 
225
392
  ---
226
393
 
227
- ## BNF Specification
394
+ ## Bilingual Pipe Commands (日本語対応)
395
+
396
+ All pipe commands have Japanese aliases:
397
+
398
+ | English | 日本語 | Description |
399
+ |---------|--------|-------------|
400
+ | `spawn` | `生成` | Create agent from value |
401
+ | `perceive` | `知覚` | Agent observes environment |
402
+ | `decide` | `判断` | Agent chooses action |
403
+ | `act` | `行動` | Agent executes decision |
404
+ | `agent_sigma` | `自律σ` | Agent metadata |
405
+ | `mediate` | `調停` | Concurrent round execution |
406
+ | `mediate_run` | `調停実行` | Multi-round execution |
407
+ | `mediator_sigma` | `調停σ` | Mediator statistics |
408
+ | `mediate_strategy` | `調停戦略` | Set conflict strategy |
409
+ | `mediate_message` | `調停通信` | Point-to-point message |
410
+ | `mediate_broadcast` | `調停放送` | Broadcast to all agents |
411
+ | `emit` | `発火` | Emit event |
412
+ | `subscribe` | `購読` | Subscribe to events |
413
+ | `bind` | `結合` | Create relation binding |
414
+ | `intend` | `意図` | Set entity intention |
415
+ | `compress` | `圧縮` | RCT compression |
416
+ | `decompress` | `復元` | RCT decompression |
228
417
 
229
- The complete BNF v0.2 specification is available in the repository.
418
+ ---
419
+
420
+ ## BNF Specification
230
421
 
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
422
+ The complete BNF v0.3 specification is available in [`spec/`](./spec/).
235
423
 
236
424
  ---
237
425
 
@@ -239,10 +427,15 @@ Key features integrated from 21 D-FUMT theories:
239
427
 
240
428
  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
429
 
430
+ See [`theory/`](./theory/) for the complete theoretical documentation.
431
+
242
432
  ---
243
- ☮️ Peace Use Clause / 平和利用条項
433
+
434
+ ## ☮️ Peace Use Clause / 平和利用条項
435
+
244
436
  Rei is licensed under Apache 2.0 with an additional Peace Use Clause.
245
437
  Rei は Apache 2.0 ライセンスに加え、平和利用条項が適用されます。
438
+
246
439
  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
440
  仏教の「空」の概念と D-FUMT の意識数学に基づき、Rei は人類の平和的発展のためにのみ設計されています。以下の目的での使用は禁止されています:
248
441
 
@@ -252,6 +445,7 @@ Rooted in the Buddhist concept of "Kū" (空, Emptiness) and D-FUMT's consciousn
252
445
 
253
446
  ✅ Education, research, humanitarian aid, and creative endeavors are encouraged.
254
447
  ✅ 教育・研究・人道支援・創造的活動での使用を推奨します。
255
- See PEACE_USE_CLAUSE.md for full details.
256
448
 
257
- MIT © Nobuki Fujimoto
449
+ See [PEACE_USE_CLAUSE.md](./PEACE_USE_CLAUSE.md) for full details.
450
+
451
+ Apache 2.0 © Nobuki Fujimoto
package/bin/rei.js CHANGED
@@ -13,7 +13,7 @@ const fs = require('fs');
13
13
  const path = require('path');
14
14
  const readline = require('readline');
15
15
 
16
- const VERSION = '0.3.0';
16
+ const VERSION = '0.5.0';
17
17
 
18
18
  // --- Result formatting ---
19
19
  function formatResult(val) {