cella-lang 0.1.202610121721 → 0.1.202610130348

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/CHANGES.md CHANGED
@@ -7,6 +7,7 @@
7
7
 
8
8
  ### 新增
9
9
 
10
+ - 直譯器與除錯器:`compile_wasm_library(source, exports)`(函式庫模式:匯出指定的定義,宿主一次呼叫一個,值以不透明的參照進出);`cella-lang/host`(`loadLibrary`);`cella-lang/dap`(`createSession`:DAP 形狀的除錯工作階段——斷點、單步、跳出、暫停、呼叫堆疊、變數、改變數、補輸入,停止原因含等輸入、未定義行為、未接住的例外)。說明見 README 的 *Running a program step by step*。
10
11
  - `DecEq.uip`(hlevel):相等可判定 ⟹ 相等的證明唯一(`a b : x = y` 時 `a = b`;不需要 K)。
11
12
  - `auto` 在 `a = b` 目標上,兩邊定義上相等時證得出。
12
13
  - `DecEq A`:`A` 的相等可判定(`(x y : A) -> Dec (x = y)`,同 Lean 的 `DecidableEq`)。
@@ -24,6 +25,8 @@
24
25
 
25
26
  ### ⚠️ 不相容
26
27
 
28
+ - `IsModularLaw`(algebra)改成真的模組律——`meet a c = a`(a ≤ c)時 `join a (meet b c) = meet (join a b) c`;之前的定義是無條件的分配律,與名字不符。
29
+ - foundations 的 `ΣMap` 改名 `sigmaMapSnd`(對第二分量映射;與 sigma 模組原本的 `sigmaMapSnd` 是同一個,那一份刪掉)。algebra 的 `orTrueLV2`、`andFalseLV2` 刪掉,用 bool 的 `orTrueL`、`andFalseL`。
27
30
  - 標準庫同一模組裡陳述相同(只差變數名)的定理合併(145 個):保留在檔裡排最前面的那一個,其餘刪掉、使用處改用保留的名字(`negSucInt` → `negSuc`、`mulCommNat` → `mulComm`、`listAppendRightId` → `myAppendNil`……;完整對照在 commit 訊息附帶的清單)。合併後會形成遞迴循環的一組(`reverseLength`)保留。
28
31
  - 標準庫跨模組的重複引理合併(13 個):帶後綴的那份所在模組已(遞移地)匯入基底版本時,刪掉帶後綴的、使用處改用基底(`beqReflV2` → `beqRefl`、`appendAssocV2` → `appendAssoc`、`lengthMapAlias` → `lengthMap`……);`mapSingletonStd`(std)與 `mapSingleton`(list)反過來,保留 std 的那份、名叫 `mapSingleton`。
29
32
  - 標準庫名字裡的舊版本後綴拿掉(`V140`、`V2`、`_v2`、`Std`、`Alias`):拿掉後不撞名的直接用基底名字(109 個,例:`listBindReturnV140` → `listBindReturn`;兩個會撞名的 Σ 版改叫 `transportReflSigmaPair`);同一檔裡陳述相同的版本只留一份(11 個);以 `Tac`/`Test`/`Example` 結尾的例子搬到 `stdlib/tests`(51 個)。跨檔的重複與陳述不同的暫留。
@@ -64,6 +67,16 @@
64
67
 
65
68
  ### 修正
66
69
 
70
+ - 除錯器(`cella-lang/dap`):未接住的例外帶著丟出它的節點時(例如 `@cella-lang/python` 的例外),`stopped` 的 `nodeId` 是那個節點。
71
+ - 樣式裡引數位置的零引數建構子照**那個欄位的型別**認:`just zero`(`zero` 是 import 進來的 Nat 的建構子)以前被當成 `just <變數 zero>`,接住所有的 `just _`——後面接 `_` 的分支永遠到不了,程式的意思被改掉。和別的型別的建構子同名的變數(`mkPair acc rem`)照舊是變數。
72
+ - 對 match 的分支一般化依賴的假設(convoy)時,期望型別「經由一個忽略它的定義」提到假設(`f x h`,`f` 不看 `h`)也算提到:之前判成沒提到、不一般化,產出的 motive 型別不對,第二個 kernel 拒絕。
73
+ - 由一個證明的型別推函式的隱式參數,而那個型別已化簡成很大的中性項時(例如具體的指標、記憶體),不再用光整條定義的預算。
74
+ - 引數不合的型別錯誤不再因為「先以期望型別定隱式參數」的重試而變慢、變成 unknown。
75
+ - `fst r.v`、`snd r.v` 的點投影屬於引數(`fst (r.v)`),和一般函式一致;之前剖析成 `(fst r).v`。
76
+ - 檢查時不再算出用不到的值:封閉的定義(`def x := 要算很久的計算`)、對它取欄位(`(…).f`)或 `match` 它(期望型別裡不會出現它時),之前都在檢查時把它算完、可能用完預算判成 unknown。
77
+ - 沒寫 `mutual`、靠向前參照形成的互遞迴:第二個 kernel 照常展開(之前整組不展開,elaborator 接受的等式被它拒絕)。
78
+ - 瀏覽器版的 `compile_wasm`:用到程式自己宣告的型別(`data`、`record`)的程式不再被拒(unknown constructor)。
79
+ - 編譯執行(WasmGC、VM):`cond b x y` 這種選擇子只算走到的那一支(之前兩支都算,遞迴時是指數時間)。
67
80
  - 遞迴定義的本體用完整名稱呼叫自己(`N.beq j k`)也算遞迴:之前只認裸名,限定名的呼叫被當成不遞迴,依賴它展開的證明被第二個 kernel 拒絕。
68
81
  - `transport` 的引數照一般引數剖析點號(`transport p B.tt`、`transport Path.refl a`)。
69
82
  - explicit 模式下 `import std` 只帶 std 與它的依賴:從快照還原後之前會帶進全部預載模組。快照記下每個模組直接 import 了誰。
@@ -78,6 +91,8 @@
78
91
  - 可展開的定義(不遞迴的函式、常數)在隱式參數的解、型別提示、判決的項裡保留折疊的寫法(`common p s t`),不再展開成本體;需要時才展開。快照因此變小(cpp 套件 3.50 → 3.29 MB),用到這類隱式參數的定義雜湊改變。
79
92
  - 很長的串列與自己比較不再判成 unknown:`replicate 2000 tt ≡ replicate 2000 tt := refl` 之前因為第二個檢查器比較的深度上限而是 unknown(訊息寫「用完步數」),現在接受(到 50000 都是)。
80
93
 
94
+
95
+
81
96
  ## 0.1.202610100531(2026-10-10)
82
97
 
83
98
  ### 修正
package/README.md CHANGED
@@ -56,6 +56,56 @@ check('import cpp\ndef x : cpp.Int32 := cpp.lit (pos 5) refl');
56
56
 
57
57
  `checker.hash` in every verdict identifies the exact build; pin the package version.
58
58
 
59
+ ## Running a program step by step (debugger)
60
+
61
+ Cella programs can be compiled in **library mode**: instead of running `main`, the module exports the
62
+ definitions you name, and the host calls them one at a time. Values cross the boundary as opaque references.
63
+ `cella-lang/dap` builds a debug session on top of it, shaped like the Debug Adapter Protocol.
64
+
65
+ 以函式庫模式編譯:不跑 `main`,匯出指定的定義,宿主一次呼叫一個;值以不透明的參照進出。`cella-lang/dap` 在上面做出 DAP 形狀的除錯工作階段。
66
+
67
+ ```js
68
+ import init, { init_stdlib_cached, load_library_pack, compile_wasm_library } from 'cella-lang';
69
+ import { createSession, DAP_EXPORTS } from 'cella-lang/dap';
70
+
71
+ // … init and load the stdlib and @cella-lang/cpp as in "Three modes" …
72
+ const bytes = compile_wasm_library(source, DAP_EXPORTS.join(',')); // throws a JSON string on errors
73
+ const session = await createSession(bytes);
74
+ session.onEvent((event, body) => { /* 'stopped' | 'output' | 'terminated' | 'exited' */ });
75
+ session.setBreakpoints(['n12', 'n40']); // node ids (the labels of `cpp.sLabel`)
76
+ await session.launch({ stopOnEntry: false }); // runs to a breakpoint
77
+ session.stackTrace(); // [{id, fn, nodeId}], innermost first
78
+ session.variables(0); // [{id, name, type, value, declNode}] of frame 0; 'globals' for globals
79
+ session.setVariable(1, 'main.x', '42'); // {ok: true} or {ok: false, message}
80
+ await session.stepIn(); // also next(), stepOut(), continue(), pause(), terminate()
81
+ await session.supplyInput('5\n'); // after a stop with reason 'awaiting-input'; closeInput() for EOF
82
+ session.readEnv(); // the environment's state (e.g. @cella-lang/arduino: pins, servos, LCD, clock); null for plain programs
83
+ session.setInput('A0', '512'); // change an environment input while paused (sensors), effective from the next read
84
+ session.trace(); // supplied input and input changes, by stop: replaying them gives the same run
85
+ ```
86
+
87
+ **Stop reasons** (`stopped` events): `breakpoint`, `step`, `entry`, `pause`, `awaiting-input` (the program
88
+ read past the input given so far; supply more or close it), `undefined-behavior` (`detail`: the precondition,
89
+ the clause of the standard, the values and the failing operation; the program ends), `exception` (an uncaught
90
+ exception, `detail`: its type and value; the program ends). A normal end is an `exited` event. `output` events
91
+ carry `{category: 'stdout' | 'stderr', output}` as the program prints.
92
+
93
+ 停止原因:斷點、單步、進入、暫停、等輸入(讀到目前輸入的結尾;補輸入或關閉)、未定義行為(細節:前置條件、標準條文、值、失敗的運算;程式結束)、未接住的例外(細節:型別與值;程式結束);正常結束是 `exited` 事件。
94
+
95
+ **What the program provides.** The session expects these exports (`DAP_EXPORTS`): `init`, `run`, `stopConf`,
96
+ `stopNode`, `stopOut`, `stopErr`, `stopDetail`, `feed`, `globals`, `frames`, `setVarErr`, `setVarConf`, `env`
97
+ (plus `setInput` when the environment has inputs). Each is a
98
+ one-line wrapper over `@cella-lang/cpp`'s debugger functions applied to a `cpp.Debugger` record, which holds the
99
+ program (statements wrapped in `cpp.sLabel` at every stoppable node; calls written `cpp.sCall fn site …` with the
100
+ node ids of the function definition and of the call), the initial environment and variables (the state is
101
+ `cpp.St E V = { io; env; vars }`), the node id of `main`, and how to print the environment and print and change variables (`cpp.showValC`, `cpp.parseValC`). A complete example is `tests/t_sem_dap.cella` in
102
+ `@cella-lang/cpp`.
103
+
104
+ 程式要匯出上面這一組名字:每個都是 `@cella-lang/cpp` 的除錯函數套在 `cpp.Debugger` 上的一行包裝(程式、變數初值、main 的節點、怎麼印與改變數)。完整的例子是 `@cella-lang/cpp` 的 `tests/t_sem_dap.cella`。
105
+
106
+ Lower level: `cella-lang/host` (`loadLibrary(bytes)` → `call`, `str`/`toStr`, `nat`/`toNat`, `tag`) for
107
+ calling any exported functions.
108
+
59
109
  ## When to upgrade
60
110
 
61
111
  `package.json` carries two fingerprints:
package/cella-dap.mjs ADDED
@@ -0,0 +1,198 @@
1
+ // Cella 直譯器的除錯工作階段,形狀照 DAP(Debug Adapter Protocol):斷點、continue、stepIn、next、stepOut、pause、terminate、
2
+ // 呼叫堆疊、變數、setVariable、補輸入,與 stopped/output/terminated/exited 事件。
3
+ // 斷點、單步、停止原因都是 Cella 裡對組態的純函數(cpp 的「除錯驅動」);這一層只呼叫它們、切時間、把結果轉成事件。
4
+ //
5
+ // 程式要以函式庫模式編譯(`cella compile --target wasm --export …`),匯出這一組名字(cpp 的 `Debugger` 加上單型的包裝,
6
+ // 範例見 cpp 套件的 `tests/t_sem_dap.cella`):
7
+ // init, run, stopConf, stopNode, stopOut, stopErr, stopDetail, feed, globals, frames, setVarErr, setVarConf, env
8
+ // 環境套件(例如 @cella-lang/arduino)另外匯出 `setInput : String -> String -> Conf -> Conf`(感測器等環境輸入)時,
9
+ // `setInput` 可用。
10
+ export const DAP_EXPORTS = ['init', 'run', 'stopConf', 'stopNode', 'stopOut', 'stopErr', 'stopDetail', 'feed', 'globals', 'frames', 'setVarErr', 'setVarConf', 'env'];
11
+
12
+ import { loadLibrary } from './cella-host.mjs';
13
+
14
+ // `cpp.Stop` 的建構子序號
15
+ const STOP = { breakpoint: 0, step: 1, fuel: 2, input: 3, undefined: 4, uncaught: 5, exited: 6 };
16
+ // 執行的種類(`cpp.dbgRun` 的 kind)
17
+ const KIND = { continue: 0, stepIn: 1, next: 2, stepOut: 3 };
18
+
19
+ /** 載入模組、建立工作階段。`fuel` 是每一段執行的步數:段與段之間交回事件迴圈(回應 pause、送出輸出)。 */
20
+ export async function createSession(bytes, { fuel = 20000 } = {}) {
21
+ const lib = await loadLibrary(bytes);
22
+ for (const name of DAP_EXPORTS) {
23
+ if (typeof lib.exports[name] !== 'function') throw new Error(`the module does not export \`${name}\` (compile it with --export ${DAP_EXPORTS.join(',')})`);
24
+ }
25
+ return new CellaDebugSession(lib, fuel);
26
+ }
27
+
28
+ export class CellaDebugSession {
29
+ constructor(lib, fuel) {
30
+ this.lib = lib;
31
+ this.fuel = fuel;
32
+ this.listeners = [];
33
+ this.breakpoints = [];
34
+ this.conf = null;
35
+ this.node = ''; // 目前停在的節點
36
+ this.sent = { stdout: 0, stderr: 0 };
37
+ this.inputClosed = false;
38
+ this.pending = null; // 等輸入時要接著做的那一種執行
39
+ this.pauseRequested = false;
40
+ this.running = false;
41
+ this.ended = false;
42
+ this.stops = 0; // 停下的次數(紀錄用:輸入與環境的改動記在「第幾次停下之後」)
43
+ this.log = []; // 補輸入與環境輸入的紀錄:重放(之後的 stepBack)照它得到相同結果
44
+ }
45
+
46
+ /** 事件:`stopped` {reason, nodeId, detail?}、`output` {category, output}、`terminated` {}、`exited` {exitCode}。 */
47
+ onEvent(cb) { this.listeners.push(cb); }
48
+ emit(event, body) { for (const cb of this.listeners) cb(event, body); }
49
+
50
+ /** 斷點:節點(nodeId)的集合;之後的執行都照新的。 */
51
+ setBreakpoints(nodeIds) { this.breakpoints = [...nodeIds]; return this.breakpoints.map((id) => ({ verified: true, nodeId: id })); }
52
+
53
+ /** 開始:`stopOnEntry` 時停在第一個節點,否則執行到斷點。 */
54
+ async launch({ stopOnEntry = false } = {}) {
55
+ const { lib } = this;
56
+ this.conf = lib.call('init', lib.nat(0));
57
+ return stopOnEntry ? this.execute(KIND.stepIn, 'entry') : this.execute(KIND.continue);
58
+ }
59
+ continue() { return this.execute(KIND.continue); }
60
+ stepIn() { return this.execute(KIND.stepIn); }
61
+ next() { return this.execute(KIND.next); }
62
+ stepOut() { return this.execute(KIND.stepOut); }
63
+ /** 在下一段燃料用完時停下(reason 'pause')。 */
64
+ pause() { if (this.running) this.pauseRequested = true; }
65
+ terminate() {
66
+ if (this.ended) return;
67
+ this.ended = true;
68
+ this.running = false;
69
+ this.emit('terminated', {});
70
+ }
71
+
72
+ /** 補輸入(接在還沒讀的輸入後面);之前因等輸入而停的那一種執行接著做。 */
73
+ supplyInput(text) {
74
+ const { lib } = this;
75
+ this.log.push({ afterStop: this.stops, kind: 'stdin', value: text });
76
+ this.conf = lib.call('feed', lib.str(text), this.conf);
77
+ return this.resumeAfterInput();
78
+ }
79
+ /** 關閉輸入(EOF):之後讀不到就照 C++ 設 eofbit。 */
80
+ closeInput() { this.inputClosed = true; return this.resumeAfterInput(); }
81
+ resumeAfterInput() {
82
+ if (this.pending === null) return Promise.resolve();
83
+ const kind = this.pending;
84
+ this.pending = null;
85
+ return this.execute(kind);
86
+ }
87
+
88
+ /** 環境(硬體等)的狀態:環境套件給的 JSON;一般程式是 null。宿主每停一次讀一次(例如接線面板)。 */
89
+ readEnv() {
90
+ if (this.conf === null) return null;
91
+ return JSON.parse(this.lib.toStr(this.lib.call('env', this.conf)));
92
+ }
93
+ /** 改環境輸入(只在暫停時;例如感測器:`setInput('A0', '512')`):從下一次讀取開始生效,並記進紀錄。 */
94
+ setInput(name, value) {
95
+ const { lib } = this;
96
+ if (typeof lib.exports.setInput !== 'function') return { ok: false, message: '這個程式的環境沒有可改的輸入' };
97
+ if (this.running || this.ended || this.conf === null) return { ok: false, message: '只能在暫停時修改' };
98
+ this.conf = lib.call('setInput', lib.str(name), lib.str(String(value)), this.conf);
99
+ this.log.push({ afterStop: this.stops, kind: 'input', name, value: String(value) });
100
+ return { ok: true };
101
+ }
102
+ /** 補輸入與環境輸入的紀錄:[{afterStop, kind: 'stdin' | 'input', name?, value}]。 */
103
+ trace() { return this.log.map((e) => ({ ...e })); }
104
+
105
+ /** 呼叫堆疊(由內往外):{id, fn(函式定義的節點), nodeId(停在哪個節點)}。 */
106
+ stackTrace() { return this.framesNow().map((f, id) => ({ id, fn: f.fn, nodeId: f.at })); }
107
+ /** 作用範圍:全域與這一層的區域。 */
108
+ scopes(frameId) { return [{ name: 'Globals', ref: 'globals' }, { name: 'Locals', ref: frameId }]; }
109
+ /** 變數:{id, name, type, value(字串;未初始化是 null), declNode}。`ref` 是 'globals' 或呼叫層的 id。 */
110
+ variables(ref) {
111
+ if (this.conf === null) return [];
112
+ if (ref === 'globals') return JSON.parse(this.lib.toStr(this.lib.call('globals', this.conf)));
113
+ return this.framesNow()[ref]?.vars ?? [];
114
+ }
115
+ /** 改變數(只在停下時):成功 {ok: true};改不了 {ok: false, message}。 */
116
+ setVariable(frameId, id, text) {
117
+ const { lib } = this;
118
+ if (this.running || this.ended || this.conf === null) return { ok: false, message: '只能在暫停時修改' };
119
+ const args = [lib.nat(frameId), lib.str(id), lib.str(text), this.conf];
120
+ const message = lib.toStr(lib.call('setVarErr', ...args));
121
+ if (message !== '') return { ok: false, message };
122
+ this.conf = lib.call('setVarConf', ...args);
123
+ return { ok: true };
124
+ }
125
+
126
+ framesNow() {
127
+ if (this.conf === null) return [];
128
+ return JSON.parse(this.lib.toStr(this.lib.call('frames', this.lib.str(this.node), this.conf)));
129
+ }
130
+
131
+ // 一種執行:一段一段跑(每段 `fuel` 步),段與段之間交回事件迴圈;停下時送出事件
132
+ async execute(kind, entryReason) {
133
+ const { lib } = this;
134
+ if (this.ended || this.conf === null) return;
135
+ this.running = true;
136
+ const bps = lib.str(this.breakpoints.join('\n'));
137
+ for (;;) {
138
+ const stop = lib.call('run', lib.nat(kind), bps, lib.nat(this.inputClosed ? 1 : 0), lib.nat(this.fuel), this.conf);
139
+ this.flushOutput(stop);
140
+ const tag = lib.tag(stop);
141
+ if (tag === STOP.fuel) {
142
+ this.conf = lib.call('stopConf', this.conf, stop);
143
+ await new Promise((r) => setTimeout(r, 0));
144
+ if (this.ended) return;
145
+ if (this.pauseRequested) {
146
+ this.pauseRequested = false;
147
+ return this.stopped('pause');
148
+ }
149
+ continue;
150
+ }
151
+ this.running = false;
152
+ if (tag === STOP.breakpoint || tag === STOP.step) {
153
+ this.conf = lib.call('stopConf', this.conf, stop);
154
+ this.node = lib.toStr(lib.call('stopNode', stop));
155
+ return this.stopped(tag === STOP.breakpoint ? 'breakpoint' : (entryReason ?? 'step'));
156
+ }
157
+ if (tag === STOP.input) {
158
+ this.conf = lib.call('stopConf', this.conf, stop);
159
+ this.pending = kind;
160
+ return this.stopped('awaiting-input');
161
+ }
162
+ // 結束:未定義行為、未接住的例外(std::terminate)、正常結束
163
+ this.ended = true;
164
+ if (tag === STOP.undefined) {
165
+ this.node = lib.toStr(lib.call('stopNode', stop));
166
+ this.stopped('undefined-behavior', JSON.parse(lib.toStr(lib.call('stopDetail', stop))));
167
+ } else if (tag === STOP.uncaught) {
168
+ // 例外帶著丟出它的節點時(其他語言的例外,例如 Python 的)停在那裡;C++ 的例外不帶,沿用最後的節點
169
+ const at = lib.toStr(lib.call('stopNode', stop));
170
+ if (at) this.node = at;
171
+ this.stopped('exception', JSON.parse(lib.toStr(lib.call('stopDetail', stop))));
172
+ } else {
173
+ this.emit('exited', { exitCode: 0 });
174
+ }
175
+ this.emit('terminated', {});
176
+ return;
177
+ }
178
+ }
179
+
180
+ stopped(reason, detail) {
181
+ this.stops += 1;
182
+ const body = { reason, nodeId: this.node };
183
+ if (detail !== undefined) body.detail = detail;
184
+ this.emit('stopped', body);
185
+ }
186
+
187
+ // 輸出:送出這次停下時比上次多出來的部分
188
+ flushOutput(stop) {
189
+ const { lib } = this;
190
+ for (const [category, fn] of [['stdout', 'stopOut'], ['stderr', 'stopErr']]) {
191
+ const all = Array.from(lib.toStr(lib.call(fn, stop)));
192
+ if (all.length > this.sent[category]) {
193
+ this.emit('output', { category, output: all.slice(this.sent[category]).join('') });
194
+ this.sent[category] = all.length;
195
+ }
196
+ }
197
+ }
198
+ }
package/cella-host.mjs ADDED
@@ -0,0 +1,68 @@
1
+ // Cella 的 WasmGC 函式庫模式的宿主(`cella compile --target wasm --export a,b`):載入模組,一次呼叫一個匯出的函數。
2
+ // 值是不透明的(WasmGC 的參照):宿主只能拿著、傳回去;要看內容就呼叫匯出的 Cella 函數,再用 `toStr`/`toNat`/`tag` 轉成 JS 的值。
3
+ // 匯入(`env.*`)與 playground 執行 `main` 時相同;函式庫的函數是純的,不做輸入輸出。
4
+
5
+ const FFI_UNAVAILABLE = 'IO is not available to library functions';
6
+
7
+ /** 載入函式庫模式的模組。回傳 `{ call, str, nat, toStr, toNat, tag, exports }`。 */
8
+ export async function loadLibrary(bytes) {
9
+ let reg = []; // 宿主 → wasm 的字串(`str_in` 經 `line_char` 一個字元一個字元取)
10
+ let cur = ''; // wasm → 宿主的字串(`str_char`/`str_end`)
11
+ const sent = [];
12
+ let error = null;
13
+ const env = {
14
+ put_nat: () => {}, put_char: () => {}, put_line: () => {},
15
+ error: (code) => { error = code; },
16
+ read_line: () => -1n,
17
+ line_char: (i) => BigInt(reg[Number(i)]?.codePointAt(0) ?? 0),
18
+ read_nat: () => 0n,
19
+ big_op: (op, a, b) => {
20
+ switch (op) {
21
+ case 0: return a + b;
22
+ case 1: return a >= b ? a - b : 0n;
23
+ case 2: return a * b;
24
+ case 3: return b === 0n ? 0n : a / b;
25
+ default: return b === 0n ? a : a % b;
26
+ }
27
+ },
28
+ big_cmp: (op, a, b) => ((op === 0 ? a === b : op === 1 ? a < b : a <= b) ? 1 : 0),
29
+ big_from: (n) => BigInt.asUintN(64, n),
30
+ big_small: (a) => (a <= 0x7fffffffffffffffn ? a : -1n),
31
+ put_big: () => {},
32
+ big_lit: () => { const n = BigInt(cur); cur = ''; return n; },
33
+ str_char: (cp) => { cur += String.fromCodePoint(cp); },
34
+ str_end: () => { sent.push(cur); cur = ''; },
35
+ ffi: () => { throw new Error(FFI_UNAVAILABLE); },
36
+ prog_arg: () => 0n,
37
+ ffi_result: () => 0n,
38
+ exit: () => { throw new Error(FFI_UNAVAILABLE); },
39
+ };
40
+ const { instance } = await WebAssembly.instantiate(bytes, { env });
41
+ const ex = instance.exports;
42
+ const guard = (f) => {
43
+ error = null;
44
+ try {
45
+ return f();
46
+ } catch (e) {
47
+ throw new Error(error !== null ? `cella runtime error ${error}` : String(e?.message ?? e));
48
+ }
49
+ };
50
+ return {
51
+ exports: ex,
52
+ /** 呼叫匯出的 Cella 函數(引數與結果都是不透明的值)。 */
53
+ call: (name, ...args) => {
54
+ if (typeof ex[name] !== 'function') throw new Error(`no exported function \`${name}\``);
55
+ return guard(() => ex[name](...args));
56
+ },
57
+ /** JS 字串 → Cella 的 `String`。 */
58
+ str: (s) => { reg = Array.from(s); return guard(() => ex.str_in(BigInt(reg.length))); },
59
+ /** JS 的整數 → Cella 的 `Nat`。 */
60
+ nat: (n) => guard(() => ex.nat_in(BigInt(n))),
61
+ /** Cella 的 `String` → JS 字串。 */
62
+ toStr: (v) => { guard(() => ex.str_out(v)); return sent.pop(); },
63
+ /** Cella 的 `Nat` → BigInt。 */
64
+ toNat: (v) => guard(() => ex.nat_out(v)),
65
+ /** 資料值的建構子序號(宣告順序):分辨 `Bool`(宣告是 `true | false`:true 0、false 1)、停止原因等。 */
66
+ tag: (v) => guard(() => ex.data_tag(v)),
67
+ };
68
+ }
package/cella.d.ts CHANGED
@@ -16,6 +16,12 @@ export function check(source: string): string;
16
16
  */
17
17
  export function compile_wasm(source: string): Uint8Array;
18
18
 
19
+ /**
20
+ * 函式庫模式(宿主一次呼叫一個函數,例如除錯器):匯出入口裡的這些定義(以逗號分隔),不跑 `main`。
21
+ * 失敗時丟出錯誤字串(同 `compile_wasm`)。宿主見 `cella-lang/host`、`cella-lang/dap`。
22
+ */
23
+ export function compile_wasm_library(source: string, exports: string): Uint8Array;
24
+
19
25
  /**
20
26
  * F319:游標處的證明目標(1 起算的行、列,字元)。回傳 `{"goals": 文字或 null}`。
21
27
  */
@@ -128,6 +134,7 @@ export interface InitOutput {
128
134
  readonly candidates: (a: number, b: number, c: number, d: number, e: number) => [number, number];
129
135
  readonly check: (a: number, b: number) => [number, number];
130
136
  readonly compile_wasm: (a: number, b: number) => [number, number, number, number];
137
+ readonly compile_wasm_library: (a: number, b: number, c: number, d: number) => [number, number, number, number];
131
138
  readonly goals_at: (a: number, b: number, c: number, d: number) => [number, number];
132
139
  readonly holes: (a: number, b: number) => [number, number];
133
140
  readonly imported_modules: (a: number, b: number) => [number, number];
package/cella.js CHANGED
@@ -62,6 +62,27 @@ export function compile_wasm(source) {
62
62
  return v2;
63
63
  }
64
64
 
65
+ /**
66
+ * 函式庫模式(宿主一次呼叫一個函數,例如除錯器):匯出入口裡的這些定義(以逗號分隔),不跑 `main`。
67
+ * 失敗時丟出錯誤字串(同 `compile_wasm`)。宿主見 `cella-lang/host`、`cella-lang/dap`。
68
+ * @param {string} source
69
+ * @param {string} exports
70
+ * @returns {Uint8Array}
71
+ */
72
+ export function compile_wasm_library(source, exports) {
73
+ const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
74
+ const len0 = WASM_VECTOR_LEN;
75
+ const ptr1 = passStringToWasm0(exports, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
76
+ const len1 = WASM_VECTOR_LEN;
77
+ const ret = wasm.compile_wasm_library(ptr0, len0, ptr1, len1);
78
+ if (ret[3]) {
79
+ throw takeFromExternrefTable0(ret[2]);
80
+ }
81
+ var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
82
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
83
+ return v3;
84
+ }
85
+
65
86
  /**
66
87
  * F319:游標處的證明目標(1 起算的行、列,字元)。回傳 `{"goals": 文字或 null}`。
67
88
  * @param {string} source
package/cella_bg.wasm CHANGED
Binary file
@@ -4,6 +4,7 @@ export const memory: WebAssembly.Memory;
4
4
  export const candidates: (a: number, b: number, c: number, d: number, e: number) => [number, number];
5
5
  export const check: (a: number, b: number) => [number, number];
6
6
  export const compile_wasm: (a: number, b: number) => [number, number, number, number];
7
+ export const compile_wasm_library: (a: number, b: number, c: number, d: number) => [number, number, number, number];
7
8
  export const goals_at: (a: number, b: number, c: number, d: number) => [number, number];
8
9
  export const holes: (a: number, b: number) => [number, number];
9
10
  export const imported_modules: (a: number, b: number) => [number, number];
Binary file
Binary file
Binary file
package/modules/fin.cell CHANGED
Binary file
Binary file
Binary file
@@ -1,34 +1,34 @@
1
1
  {
2
- "source_hash": "144e5777672d9d69064f6f6f8f04d6559ffd80c342bc1c2546ce28ec65f35a99",
2
+ "source_hash": "df01af631908958acc17937b8df6c4f6e5be71c4f349ce5aff06b178f7cb4990",
3
3
  "modules": {
4
- "algebra": {"deps": ["int", "nat", "ring", "algebra"], "bytes": 256105},
5
- "decidable": {"deps": ["decidable"], "bytes": 195099},
6
- "either": {"deps": ["either"], "bytes": 137399},
7
- "fin": {"deps": ["fin"], "bytes": 97984},
8
- "function": {"deps": ["function"], "bytes": 150437},
9
- "functor": {"deps": ["functor"], "bytes": 127044},
10
- "int": {"deps": ["int"], "bytes": 220479},
11
- "intlinarith": {"deps": ["nat", "ring", "int", "intring", "linarith", "intlinarith"], "bytes": 230398},
12
- "intomega": {"deps": ["nat", "ring", "linarith", "omega", "int", "intring", "intlinarith", "intomega"], "bytes": 199890},
13
- "intring": {"deps": ["nat", "ring", "int", "intring"], "bytes": 204636},
14
- "linarith": {"deps": ["nat", "ring", "linarith"], "bytes": 145614},
15
- "linsys": {"deps": ["nat", "ring", "int", "intring", "linsys"], "bytes": 212256},
16
- "list": {"deps": ["list"], "bytes": 184363},
17
- "map": {"deps": ["ord", "map"], "bytes": 111376},
18
- "meta": {"deps": ["option", "meta"], "bytes": 143598},
19
- "monad": {"deps": ["functor", "monad"], "bytes": 143077},
20
- "nat": {"deps": ["nat"], "bytes": 137927},
21
- "omega": {"deps": ["nat", "ring", "linarith", "omega"], "bytes": 131406},
22
- "option": {"deps": ["option"], "bytes": 172053},
23
- "ord": {"deps": ["ord"], "bytes": 92984},
24
- "ordering": {"deps": ["ord", "decidable", "int", "ordering"], "bytes": 307461},
25
- "polyrith": {"deps": ["nat", "ring", "int", "intring", "linarith", "intlinarith", "polyrith"], "bytes": 182372},
26
- "prod": {"deps": ["prod"], "bytes": 158018},
27
- "ring": {"deps": ["nat", "ring"], "bytes": 121864},
28
- "sigma": {"deps": ["sigma"], "bytes": 133145},
29
- "tree": {"deps": ["tree"], "bytes": 116864},
30
- "unit": {"deps": ["unit"], "bytes": 121472},
31
- "vector": {"deps": ["fin", "vector"], "bytes": 116991},
32
- "winding": {"deps": ["int", "decidable", "winding"], "bytes": 253856}
4
+ "algebra": {"deps": ["int", "nat", "ring", "algebra"], "bytes": 255329},
5
+ "decidable": {"deps": ["decidable"], "bytes": 195111},
6
+ "either": {"deps": ["either"], "bytes": 137411},
7
+ "fin": {"deps": ["fin"], "bytes": 97996},
8
+ "function": {"deps": ["function"], "bytes": 150449},
9
+ "functor": {"deps": ["functor"], "bytes": 127056},
10
+ "int": {"deps": ["int"], "bytes": 220491},
11
+ "intlinarith": {"deps": ["nat", "ring", "int", "intring", "linarith", "intlinarith"], "bytes": 230410},
12
+ "intomega": {"deps": ["nat", "ring", "linarith", "omega", "int", "intring", "intlinarith", "intomega"], "bytes": 199902},
13
+ "intring": {"deps": ["nat", "ring", "int", "intring"], "bytes": 204648},
14
+ "linarith": {"deps": ["nat", "ring", "linarith"], "bytes": 145626},
15
+ "linsys": {"deps": ["nat", "ring", "int", "intring", "linsys"], "bytes": 212268},
16
+ "list": {"deps": ["list"], "bytes": 184375},
17
+ "map": {"deps": ["ord", "map"], "bytes": 111388},
18
+ "meta": {"deps": ["option", "meta"], "bytes": 143610},
19
+ "monad": {"deps": ["functor", "monad"], "bytes": 143089},
20
+ "nat": {"deps": ["nat"], "bytes": 137939},
21
+ "omega": {"deps": ["nat", "ring", "linarith", "omega"], "bytes": 131418},
22
+ "option": {"deps": ["option"], "bytes": 172065},
23
+ "ord": {"deps": ["ord"], "bytes": 92996},
24
+ "ordering": {"deps": ["ord", "decidable", "int", "ordering"], "bytes": 307473},
25
+ "polyrith": {"deps": ["nat", "ring", "int", "intring", "linarith", "intlinarith", "polyrith"], "bytes": 182384},
26
+ "prod": {"deps": ["prod"], "bytes": 158030},
27
+ "ring": {"deps": ["nat", "ring"], "bytes": 121876},
28
+ "sigma": {"deps": ["sigma"], "bytes": 132637},
29
+ "tree": {"deps": ["tree"], "bytes": 116876},
30
+ "unit": {"deps": ["unit"], "bytes": 122046},
31
+ "vector": {"deps": ["fin", "vector"], "bytes": 117003},
32
+ "winding": {"deps": ["int", "decidable", "winding"], "bytes": 253868}
33
33
  }
34
34
  }
package/modules/int.cell CHANGED
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/modules/list.cell CHANGED
Binary file
package/modules/map.cell CHANGED
Binary file
package/modules/meta.cell CHANGED
Binary file
Binary file
package/modules/nat.cell CHANGED
Binary file
Binary file
Binary file
package/modules/ord.cell CHANGED
Binary file
Binary file
Binary file
package/modules/prod.cell CHANGED
Binary file
package/modules/ring.cell CHANGED
Binary file
Binary file
package/modules/tree.cell CHANGED
Binary file
package/modules/unit.cell CHANGED
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cella-lang",
3
- "version": "0.1.202610121721",
3
+ "version": "0.1.202610130348",
4
4
  "description": "The Cella proof checker (cubical type theory with QTT) compiled to WebAssembly — browsers and Node.",
5
5
  "type": "module",
6
6
  "main": "cella.js",
@@ -8,15 +8,17 @@
8
8
  "exports": {
9
9
  ".": { "types": "./cella.d.ts", "default": "./cella.js" },
10
10
  "./assets": "./assets.js",
11
+ "./host": "./cella-host.mjs",
12
+ "./dap": "./cella-dap.mjs",
11
13
  "./corpus.json": "./corpus.json",
12
14
  "./package.json": "./package.json"
13
15
  },
14
16
  "files": ["cella.js", "cella_bg.wasm", "cella.d.ts", "cella_bg.wasm.d.ts",
15
- "stdlib.cell", "modules/", "assets.js", "corpus.json", "README.md", "CHANGES.md", "LICENSE", "NOTICE"],
17
+ "stdlib.cell", "modules/", "assets.js", "cella-host.mjs", "cella-dap.mjs", "corpus.json", "README.md", "CHANGES.md", "LICENSE", "NOTICE"],
16
18
  "keywords": ["cella", "proof", "type-theory", "cubical", "wasm", "theorem-prover"],
17
19
  "homepage": "https://timcsy.github.io/cella-playground/",
18
20
  "license": "Apache-2.0",
19
21
  "author": "SungYu Chang",
20
22
  "engines": { "node": ">=18" },
21
- "cella": { "checkerHash": "12195e22232883d8", "semanticsHash": "4b8486c7774835a7", "filesHash": "6886b9b926121504", "commit": "49603df324b1a1223c238e074ecf5d3558d63020" }
23
+ "cella": { "checkerHash": "a1de66b317d1c8d3", "semanticsHash": "4b8486c7774835a7", "filesHash": "6d26c0ef83dd3f67", "commit": "4d8f07f10a4c690e807b8d3de35346e03e9b8525" }
22
24
  }
package/stdlib.cell CHANGED
Binary file