Compare commits

...
24 Commits
Author SHA1 Message Date
cflmyandCursor 2e068e66d4 release: v0.3.6 — Mid stdlib M1–M6
Ship mid-tier libraries through log/uuid, bump Cargo/README/CHANGELOG, and refresh the Release workflow highlights.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:55:26 +08:00
cflmyandCursor a4c346d4a2 fix(agent,llm): avoid host_* writeback and tolerate SSE reasoning
Alias writeback as wb so the writeback= param does not shadow lib calls; ignore unknown stream event types so live SSE models keep working.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:52:44 +08:00
cflmyandCursor fe14d1f7e3 fix(agent): route writeback through lib instead of host_*
ext/** must not call host_*; use writeback.record / 自写回.写回 so the host-boundary gold passes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:40:24 +08:00
cflmyandCursor f4b39dfe80 feat(stdlib): Mid M6 log levels and UUID v4
Close the mid-tier stdlib waves with filtered one-line logs and cryptographic UUID minting in lib/, plus golds and public demos.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:37:28 +08:00
cflmyandCursor 7d86ed1d9c feat(stdlib): Mid M5 list algorithms and cli.parse
Thicken table with sort/unique/chunk/zip/flatten and add lib/cli
flag parsing, with golds and public demos.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:32:58 +08:00
cflmyandCursor 506de65dae feat(stdlib): Mid M4 csv and thickened text helpers
Add lib/csv parse/stringify and expand lib/text with contains,
replace, case, pad, and related golds plus public demos.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:27:19 +08:00
cflmyandCursor 08384c7540 feat(stdlib): Mid M3 hash digests and secure tokens
Add lib/hash and lib/secrets (EN/ZH) with known-vector golds,
getrandom-backed tokens, and public demos.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:15:36 +08:00
cflmyandCursor 94ae6713aa feat(stdlib): Mid M2 path algebra and fs copy/move/temp
Add lib/path (EN/ZH) and thicken fs with sandboxed copy_file, move,
and make_temp, with golds and public demos.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:10:12 +08:00
cflmyandCursor 838a2b425a feat(stdlib): Mid M1 regex and encoding libraries
Add lib/re and lib/encoding (EN/ZH) backed by host_re_* and
host_encoding_*, with golds, public demos, and design docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 20:02:43 +08:00
cflmyandCursor 1a5ee57a6f docs(stdlib): plan mid-tier standard library waves M1–M6
Define mid-stdlib admission rules and a phased roadmap so general
primitives land in lib/ instead of ABI plugins.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 19:53:44 +08:00
cflmyandCursor d539e6b96b fix(pages): build linalg plugin for public site CI
public/features/07-linalg needs marqdo_plugin_linalg; pages only built quantum.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 19:35:52 +08:00
cflmyandCursor 3ade036d34 feat(linalg): F3 formula-doc public pages and T_ascii
Ship runnable public how-to pages, T_ascii/转置文本 one-step display,
explicit auto-simplify, and mark formula-doc F1–F3 complete.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 16:13:22 +08:00
cflmyandCursor f3c49e86ae feat(linalg): F2 declare from GFM table and lecture example
Add la.declare for batch MatrixSymbol from @-record tables, ship
examples/linalg-transpose, and mark formula-doc F2 done.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 15:52:46 +08:00
cflmyandCursor b2401b8458 docs(linalg): formula-doc design and F1 display auto-simplify
Lock the run-implies-correct authoring model, default-simplify ascii/latex/show
(with raw= escape), and add linalg-doc-smoke for (AB)^T identity.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 15:50:33 +08:00
cflmyandCursor 51bb8767e3 feat(linalg): matrix infix ops and # matrix methods
Dispatch +/−/* to the linalg plugin for matrix values, retag for EN/ZH
method chains, and expand # matrix with T/simplify/ascii-style methods.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 15:13:47 +08:00
cflmyandCursor c0fbe9b182 feat(linalg): L6 lstsq, norms, complex dtype, draw themes
Add QR/SVD least squares, norm/cond/rank, complex dense Frobenius
baseline, and light/dark/bw draw themes aligned with quantum Q8.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:47:30 +08:00
cflmyandCursor 01287d9e7e feat(linalg): L5 examples, public feature pages, and skill docs
Ship SVD / least-squares examples, heatmap-hinton draw paths, coerce
dense factors into MatExpr ops, and document ext/linalg in skill/README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:36:17 +08:00
cflmyandCursor 70fb2fbfb2 feat(linalg): L4 factorize and structure SVG draw
Add LU/QR/SVD/eig/chol dense factorizations, teaching structure SVGs,
LU reuse via solve(factor=), and linalg-factor-smoke gold.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:31:44 +08:00
cflmyandCursor b6f116108c feat(linalg): L3 block collapse and lazy Kronecker
Add Block/Kron MatExpr, collapse for Block*Block, R7 flatten,
explicit assembly/kronecker, and linalg-block-kron smoke.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:22:08 +08:00
cflmyandCursor 03aca7cdac feat(linalg): L2 dense explicit, det, trace, and solve
Add dense kernels with element limits, from_formula for $$ matrices,
and linalg-basic-smoke covering 2×2 det/trace/solve/explicit.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:16:16 +08:00
cflmyandCursor 96da88e168 feat(linalg): L0–L1 formula-first MatExpr plugin
Add plugins/linalg with symbol/mul/transpose/inv simplify (R1–R5),
EN/ZH L1, CATALOG/ext add wiring, release native assets, and smokes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 14:10:54 +08:00
cflmyandCursor b6cceab218 docs(linalg): design formula-first linear algebra extension
Add research, design, and roadmap for ext/linalg; cross-link from
stdlib-math and quantum Q7 so classic MatExpr stays out of lib/math.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-07 10:53:56 +08:00
cflmyandCursor 95b16be714 release: v0.3.5 — prebuilt native plugins for ext add
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 17:08:19 +08:00
cflmyandCursor d31ad79bc0 feat(ext): prebuilt native plugins and ext add download
Ship Release native zips and let ext add fetch L1/native without a local Rust toolchain.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 16:57:43 +08:00
146 changed files with 10770 additions and 171 deletions

No files matched your search

+3 -1
View File
@@ -33,6 +33,8 @@ Produced by `.github/workflows/release.yml` on **windows-latest**:
| `marqdo-VER-x86_64-pc-windows-msvc.zip` | `marqdo.exe` + `lib/` | | `marqdo-VER-x86_64-pc-windows-msvc.zip` | `marqdo.exe` + `lib/` |
| `marqdo-VER-stdlib.zip` | `lib/` only | | `marqdo-VER-stdlib.zip` | `lib/` only |
| `marqdo-VER-ext.zip` | Official `ext/` L1 | | `marqdo-VER-ext.zip` | Official `ext/` L1 |
| `marqdo-VER-native-x86_64-pc-windows-msvc.zip` | Prebuilt Windows plugins (`native/*.dll`) |
| `marqdo-VER-native-x86_64-unknown-linux-gnu.zip` | Prebuilt Linux plugins (`native/lib*.so`) |
| `marqdo-VER-source.zip` | `git archive` of tagged commit | | `marqdo-VER-source.zip` | `git archive` of tagged commit |
| `marqdo-VER.vsix` | VS Code / Cursor (from branch `vscode-extension`) | | `marqdo-VER.vsix` | VS Code / Cursor (from branch `vscode-extension`) |
| `marqdo-VER-public.zip` | Static user docs (`marqdo view output public`) | | `marqdo-VER-public.zip` | Static user docs (`marqdo view output public`) |
@@ -164,7 +166,7 @@ git checkout vVER
cargo build --release cargo build --release
./target/release/marqdo version ./target/release/marqdo version
cargo build --release -p marqdo_plugin_web -p marqdo_plugin_agent -p marqdo_plugin_quantum cargo build --release -p marqdo_plugin_web -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg
marqdo ext add web && marqdo ext add agent && marqdo ext add quantum marqdo ext add web && marqdo ext add agent && marqdo ext add quantum
# 中文 id:网页 / 智能体 / 量子 # 中文 id:网页 / 智能体 / 量子
``` ```
+21 -6
View File
@@ -4,10 +4,11 @@ description: >-
Author and edit Marqdo programs (.mq.md): Markdown markers are executable Author and edit Marqdo programs (.mq.md): Markdown markers are executable
syntax (functions, calls, statements, returns, control flow). Use when writing syntax (functions, calls, statements, returns, control flow). Use when writing
or modifying .mq.md files, teaching Marqdo, generating Marqdo from prose, or modifying .mq.md files, teaching Marqdo, generating Marqdo from prose,
importing lib/* or ext/web or ext/quantum, running marqdo CLI, building dynamic importing lib/* or ext/web or ext/quantum or ext/linalg, running marqdo CLI,
sites with ext/web, quantum circuits with ext/quantum, or when the user building dynamic sites with ext/web, quantum circuits with ext/quantum,
mentions Marqdo, mq.md, markup-as-syntax, code-as-documentation, web/网页, or formula-first linear algebra with ext/linalg, or when the user mentions
quantum/量子 extension. Marqdo, mq.md, markup-as-syntax, code-as-documentation, web/网页, quantum/量子,
or linalg/线性代数 extension.
--- ---
# Marqdo development # Marqdo development
@@ -141,7 +142,7 @@ import clock:lib/time.mq.md
- Instance methods stay `` > `obj`.method `` (backticks on the receiver only). - Instance methods stay `` > `obj`.method `` (backticks on the receiver only).
- `lib/…` resolves via `MARQDO_LIB`, cwd `lib/`, or `lib/` next to the `marqdo` binary. - `lib/…` resolves via `MARQDO_LIB`, cwd `lib/`, or `lib/` next to the `marqdo` binary.
- Design: [module-namespace.md](../../doc/design/module-namespace.md). - Design: [module-namespace.md](../../doc/design/module-namespace.md).
- Official optional extensions (`ext/`, not stdlib): install with `marqdo ext add llm|agent|web|quantum` (see `doc/design/ext-cli.md`). `ext/llm` — chat; **`ext/agent`** — document-driven agents (see **ext/agent** below); **`ext/web`** — dynamic sites (see below); **`ext/quantum`** — circuits + Q7/Q8 (see below). - Official optional extensions (`ext/`, not stdlib): install with `marqdo ext add llm|agent|web|quantum|linalg` (see `doc/design/ext-cli.md`). `ext/llm` — chat; **`ext/agent`** — document-driven agents (see **ext/agent** below); **`ext/web`** — dynamic sites (see below); **`ext/quantum`** — circuits + Q7/Q8 (see below); **`ext/linalg`** — formula-first linear algebra (see below).
- Native plugins: `lib/plugin` — `## load` / `unload` / `list`. C ABI: `include/marqdo_abi.h`. - Native plugins: `lib/plugin` — `## load` / `unload` / `list`. C ABI: `include/marqdo_abi.h`.
- Prefer `table.put` / `表.改` for list/map element updates; keep `json` for parse/stringify/quote (see `doc/design/stdlib-table.md`). - Prefer `table.put` / `表.改` for list/map element updates; keep `json` for parse/stringify/quote (see `doc/design/stdlib-table.md`).
- Builtins (no import): `print`/`打印`, `input`/`输入`, `len`/`长度`, `str`/`文本`, `int`/`整数`; literals `True`/`真`, `False`/`假`, `None`/`空`; logic `and`/`且`, `or`/`或`, `not`/`非`. - Builtins (no import): `print`/`打印`, `input`/`输入`, `len`/`长度`, `str`/`文本`, `int`/`整数`; literals `True`/`真`, `False`/`假`, `None`/`空`; logic `and`/`且`, `or`/`或`, `not`/`非`.
@@ -225,6 +226,19 @@ Install: `marqdo ext add quantum` (ZH id: `量子`). Build: `cargo build --relea
Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7.md](../../doc/design/ext-quantum-q7.md) · Q8 viz: [ext-quantum-viz-style.md](../../doc/design/ext-quantum-viz-style.md) · examples: [quantum-bell](../../examples/quantum-bell/) · [quantum-entanglement](../../examples/quantum-entanglement/). Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7.md](../../doc/design/ext-quantum-q7.md) · Q8 viz: [ext-quantum-viz-style.md](../../doc/design/ext-quantum-viz-style.md) · examples: [quantum-bell](../../examples/quantum-bell/) · [quantum-entanglement](../../examples/quantum-entanglement/).
## Official extension: `ext/linalg` (formula-first LA)
Install: `marqdo ext add linalg` (ZH id: `线性代数`). Build: `cargo build --release -p marqdo_plugin_linalg`, then **`marqdo ext add linalg` again** so view loads the new native lib.
- EN: `import la:ext/linalg/linalg.mq.md`
- ZH: `导入 la:ext/linalg/线性代数.mq.md`
**Hard rules:** `ext/**` never calls `host_*`; hot path is ABI plugin; prefer top-level `la.symbol` (not `matrix.symbol` without a receiver); quote `kind=` strings; dense `explicit` / factorize obey size guards.
APIs (EN): `symbol` → `# matrix`;中缀 `+`/`-`/`*`;方法 `` `A`.T `` / ``.simplify`` / ``.ascii`` · `from_formula` / `explicit` / `factorize` / `draw` · `lstsq` / `norm` / `cond` / `rank`.
Examples: [linalg-svd](../../examples/linalg-svd/) · [linalg-least-squares](../../examples/linalg-least-squares/). Design: [ext-linalg.md](../../doc/design/ext-linalg.md) · formula-doc: [ext-linalg-formula-doc.md](../../doc/design/ext-linalg-formula-doc.md).
## AI authoring workflow ## AI authoring workflow
1. Decide language surface (English builtins + `lib/text.mq.md`, or Chinese + `lib/文本.mq.md`). 1. Decide language surface (English builtins + `lib/text.mq.md`, or Chinese + `lib/文本.mq.md`).
@@ -252,7 +266,8 @@ Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7
| Mix `web.page` and `网页.页面` in one file | One import language per `.mq.md` | | Mix `web.page` and `网页.页面` in one file | One import language per `.mq.md` |
| Call `host_web_*` from `ext/web` | Use `# app` / `# db` methods; plugin ABI only | | Call `host_web_*` from `ext/web` | Use `# app` / `# db` methods; plugin ABI only |
| Call `host_*` from `ext/quantum` | Use `# circuit` / `# density` methods; plugin ABI only | | Call `host_*` from `ext/quantum` | Use `# circuit` / `# density` methods; plugin ABI only |
| Bare `kind=hinton` inside `*…*` | `kind="hinton"` — quote text literals | | Call `host_*` from `ext/linalg` | Use `la.*` L1 wrappers; plugin ABI only |
| Bare `kind=eig` inside `*…*` | `kind="eig"` — quote text literals |
| Bare `theme=dark` inside `*…*` | `theme="dark"` — quote; ZH `主题="dark"` | | Bare `theme=dark` inside `*…*` | `theme="dark"` — quote; ZH `主题="dark"` |
| Rebuild plugin but skip `ext add` | `cargo build -p marqdo_plugin_quantum` then `marqdo ext add quantum` (or set `MARQDO_QUANTUM_PLUGIN`) | | Rebuild plugin but skip `ext add` | `cargo build -p marqdo_plugin_quantum` then `marqdo ext add quantum` (or set `MARQDO_QUANTUM_PLUGIN`) |
+61
View File
@@ -149,6 +149,39 @@ import text:lib/text.mq.md
> print text=`parts` > print text=`parts`
``` ```
## 7b. Mid M1 — regex + encoding (no plugin)
```markdown
---
import re:lib/re.mq.md
import enc:lib/encoding.mq.md
---
# main
*`ok` = > re.is_match text="a@b.c" pattern="@"*
*`b64` = > enc.base64_encode text="hi"*
> print text=`ok`
> print text=`b64`
```
## 7c. Mid M6 — log + uuid
```markdown
---
import log:lib/log.mq.md
import uuid:lib/uuid.mq.md
---
# main
> log.info text="start"
> log.set_level level="debug"
> log.debug text="detail"
*id = > uuid.v4*
> print text=`id`
```
## 8. Comment paragraphs (blank-line rule) ## 8. Comment paragraphs (blank-line rule)
```markdown ```markdown
@@ -326,3 +359,31 @@ import quantum:ext/quantum/quantum.mq.md
- Full lab: `examples/quantum-entanglement/`. Bell-only: `examples/quantum-bell/`. - Full lab: `examples/quantum-entanglement/`. Bell-only: `examples/quantum-bell/`.
- `theme="dark"|"light"|"bw"` (default dark) on `circuit` / `probs` / `bloch`. Quote `kind=` / `theme=`. - `theme="dark"|"light"|"bw"` (default dark) on `circuit` / `probs` / `bloch`. Quote `kind=` / `theme=`.
- After rebuilding the plugin: `marqdo ext add quantum` so view loads the new native lib. - After rebuilding the plugin: `marqdo ext add quantum` so view loads the new native lib.
## 15. Linear algebra (ext/linalg)
```markdown
---
import la:ext/linalg/linalg.mq.md
---
# main
*`A` = > la.symbol name="A" rows=2 cols=2*
*`B` = > la.symbol name="B" rows=2 cols=2*
*`C` = `A` + `B`*
*`P` = `A` * `B`*
*`s` = > `P`.T.simplify* <!-- prefer: *`Pt` = > `P`.T* then simplify -->
```
Prefer:
```markdown
*`env` = > la.declare table=`shapes`*
*`P` = env[^A] * env[^B]*
*`t` = > `P`.T_ascii*
```
- Infix `+` / `-` / `*`;`ascii` / `T_ascii` 默认化简(`raw=True` 可关)。
- Formula-doc design: [ext-linalg-formula-doc.md](../../doc/design/ext-linalg-formula-doc.md).
- Examples: `examples/linalg-transpose/`, `examples/linalg-svd/`, `examples/linalg-least-squares/`.
+24 -2
View File
@@ -12,8 +12,8 @@ marqdo view PATH # docs browser, default port 7429
marqdo debug PATH # debugger UI, default port 7430 marqdo debug PATH # debugger UI, default port 7430
marqdo catalog PATH -o .marqdo marqdo catalog PATH -o .marqdo
marqdo ext list marqdo ext list
marqdo ext add llm|agent|web|quantum marqdo ext add llm|agent|web|quantum|linalg
marqdo ext remove llm|agent|web|quantum marqdo ext remove llm|agent|web|quantum|linalg
``` ```
Diagnostics look like `path:line:col: message` (1-based line/col). Diagnostics look like `path:line:col: message` (1-based line/col).
@@ -44,6 +44,15 @@ Import one file; use **that** file’s function names.
|-----------|-----------|-------------------------| |-----------|-----------|-------------------------|
| `lib/text.mq.md` | `lib/文本.mq.md` | text helpers / 去空白·拆分·拼接… | | `lib/text.mq.md` | `lib/文本.mq.md` | text helpers / 去空白·拆分·拼接… |
| `lib/table.mq.md` | `lib/表.mq.md` | `put`/`改` + list/map(见 `doc/design/stdlib-table.md`) | | `lib/table.mq.md` | `lib/表.mq.md` | `put`/`改` + list/map(见 `doc/design/stdlib-table.md`) |
| `lib/re.mq.md` | `lib/正则.mq.md` | Mid:`is_match`/`find`/`replace`/… |
| `lib/encoding.mq.md` | `lib/编码.mq.md` | Mid:base64/hex |
| `lib/path.mq.md` | `lib/路径.mq.md` | Mid:join/normalize/… |
| `lib/hash.mq.md` | `lib/哈希.mq.md` | Mid:sha256/hmac/… |
| `lib/secrets.mq.md` | `lib/机密.mq.md` | Mid:token_hex/urlsafe |
| `lib/csv.mq.md` | `lib/逗号表.mq.md` | Mid:parse/stringify |
| `lib/cli.mq.md` | `lib/命令行.mq.md` | Mid:`parse` flags |
| `lib/log.mq.md` | `lib/日志.mq.md` | Mid:level-filtered one-line logs |
| `lib/uuid.mq.md` | `lib/标识.mq.md` | Mid:UUID v4 |
| `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM client effect helpers(GFM + `set_text`/`canvas`/…;见 marqdo-dev) | | `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM client effect helpers(GFM + `set_text`/`canvas`/…;见 marqdo-dev) |
| `lib/fs.mq.md` | `lib/文件.mq.md` | filesystem | | `lib/fs.mq.md` | `lib/文件.mq.md` | filesystem |
| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd / load_dotenv | | `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd / load_dotenv |
@@ -57,6 +66,7 @@ Import one file; use **that** file’s function names.
| `ext/agent.mq.md` | `ext/智能体.mq.md` | document-driven **step** / **plan** + OKF;示例 `examples/agent-pong` · `agent-okf-flywheel`;Wave B:[agent-framework-2026-09.md](../../doc/research/agent-framework-2026-09.md) | | `ext/agent.mq.md` | `ext/智能体.mq.md` | document-driven **step** / **plan** + OKF;示例 `examples/agent-pong` · `agent-okf-flywheel`;Wave B:[agent-framework-2026-09.md](../../doc/research/agent-framework-2026-09.md) |
| `ext/web/web.mq.md` | `ext/web/网页.mq.md` | dynamic sites — see **ext/web** section below | | `ext/web/web.mq.md` | `ext/web/网页.mq.md` | dynamic sites — see **ext/web** section below |
| `ext/quantum/quantum.mq.md` | `ext/quantum/量子.mq.md` | circuits + Q7 density/viz + Q8 themes — see **ext/quantum** below | | `ext/quantum/quantum.mq.md` | `ext/quantum/量子.mq.md` | circuits + Q7 density/viz + Q8 themes — see **ext/quantum** below |
| `ext/linalg/linalg.mq.md` | `ext/linalg/线性代数.mq.md` | formula MatExpr + factorize/draw — see **ext/linalg** below |
Open the imported `.mq.md` under `lib/` or `ext/` to see exact `##` / `#` names and parameters. Gold tests: `tests/lib/`, `tests/structure/`, `tests/ext/`. Open the imported `.mq.md` under `lib/` or `ext/` to see exact `##` / `#` names and parameters. Gold tests: `tests/lib/`, `tests/structure/`, `tests/ext/`.
@@ -98,6 +108,16 @@ Install: `marqdo ext add quantum` (`量子`). Native: `cargo build --release -p
Examples: `examples/quantum-bell/`, `examples/quantum-entanglement/`. Design: `doc/design/ext-quantum.md` · Q7: `doc/design/ext-quantum-q7.md` · Q8: `doc/design/ext-quantum-viz-style.md`. Examples: `examples/quantum-bell/`, `examples/quantum-entanglement/`. Design: `doc/design/ext-quantum.md` · Q7: `doc/design/ext-quantum-q7.md` · Q8: `doc/design/ext-quantum-viz-style.md`.
## ext/linalg (formula-first linear algebra)
Install: `marqdo ext add linalg` (`线性代数`). Native: `cargo build --release -p marqdo_plugin_linalg`, then re-run `ext add`.
| EN import | ZH import | Notes |
|-----------|-----------|-------|
| `ext/linalg/linalg.mq.md` | `ext/linalg/线性代数.mq.md` | Top-level `symbol`/`mul`/`factorize`/`draw` |
Examples: `examples/linalg-svd/`, `examples/linalg-least-squares/`. Design: `doc/design/ext-linalg.md` · formula-doc: `doc/design/ext-linalg-formula-doc.md` · roadmap: `doc/roadmap/ext-linalg.md`.
## Formula + plot (math) ## Formula + plot (math)
```markdown ```markdown
@@ -178,4 +198,6 @@ See `doc/design/call-arguments.md`.
| `doc/design/ext-quantum.md` | `ext/quantum` circuits | | `doc/design/ext-quantum.md` | `ext/quantum` circuits |
| `doc/design/ext-quantum-q7.md` | Q7 density linear algebra + advanced SVG | | `doc/design/ext-quantum-q7.md` | Q7 density linear algebra + advanced SVG |
| `doc/design/ext-quantum-viz-style.md` | Q8 themed SVG (`dark`/`light`/`bw`; circuit/probs/bloch) | | `doc/design/ext-quantum-viz-style.md` | Q8 themed SVG (`dark`/`light`/`bw`; circuit/probs/bloch) |
| `doc/design/ext-linalg.md` | `ext/linalg` formula MatExpr + factorize/draw |
| `doc/design/ext-linalg-formula-doc.md` | 公式文档面:跑通即正确 · 代数/计算两面 |
| `doc/design/view-debug.md` | view / debug hosts | | `doc/design/view-debug.md` | view / debug hosts |
+63 -13
View File
@@ -25,8 +25,10 @@ jobs:
with: with:
node-version: "20" node-version: "20"
- name: Build release - name: Build release CLI + official plugins
run: cargo build --release run: |
cargo build --release
cargo build --release -p marqdo_plugin_web -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg
- name: Checkout extension sources (branch vscode-extension only) - name: Checkout extension sources (branch vscode-extension only)
run: git fetch origin vscode-extension && git checkout origin/vscode-extension -- vscode-extension run: git fetch origin vscode-extension && git checkout origin/vscode-extension -- vscode-extension
@@ -55,6 +57,7 @@ jobs:
$vsix = "marqdo-$ver.vsix" $vsix = "marqdo-$ver.vsix"
$public = "marqdo-$ver-public.zip" $public = "marqdo-$ver-public.zip"
$extZip = "marqdo-$ver-ext.zip" $extZip = "marqdo-$ver-ext.zip"
$nativeZip = "marqdo-$ver-native-$target.zip"
Copy-Item target/release/marqdo.exe "dist/$exe" Copy-Item target/release/marqdo.exe "dist/$exe"
git archive --format=zip -o "dist/$source" HEAD git archive --format=zip -o "dist/$source" HEAD
./target/release/marqdo view output public -o public ./target/release/marqdo view output public -o public
@@ -65,11 +68,27 @@ jobs:
Copy-Item ext "$stageExt/ext" -Recurse Copy-Item ext "$stageExt/ext" -Recurse
if (Test-Path "dist/$extZip") { Remove-Item "dist/$extZip" -Force } if (Test-Path "dist/$extZip") { Remove-Item "dist/$extZip" -Force }
Compress-Archive -Path "$stageExt/*" -DestinationPath "dist/$extZip" -Force Compress-Archive -Path "$stageExt/*" -DestinationPath "dist/$extZip" -Force
# Prebuilt native plugins (no Rust required for `marqdo ext add`)
$stageNative = "dist/stage-native"
if (Test-Path $stageNative) { Remove-Item $stageNative -Recurse -Force }
New-Item -ItemType Directory -Force -Path "$stageNative/native" | Out-Null
Copy-Item target/release/web.dll "$stageNative/native/web.dll"
Copy-Item target/release/agent.dll "$stageNative/native/agent.dll"
Copy-Item target/release/quantum.dll "$stageNative/native/quantum.dll"
Copy-Item target/release/linalg.dll "$stageNative/native/linalg.dll"
if (Test-Path "dist/$nativeZip") { Remove-Item "dist/$nativeZip" -Force }
Compress-Archive -Path "$stageNative/*" -DestinationPath "dist/$nativeZip" -Force
$stage = "dist/stage-bundle" $stage = "dist/stage-bundle"
if (Test-Path $stage) { Remove-Item $stage -Recurse -Force } if (Test-Path $stage) { Remove-Item $stage -Recurse -Force }
New-Item -ItemType Directory -Force -Path $stage | Out-Null New-Item -ItemType Directory -Force -Path $stage | Out-Null
Copy-Item target/release/marqdo.exe "$stage/marqdo.exe" Copy-Item target/release/marqdo.exe "$stage/marqdo.exe"
Copy-Item lib "$stage/lib" -Recurse Copy-Item lib "$stage/lib" -Recurse
Copy-Item ext "$stage/ext" -Recurse
New-Item -ItemType Directory -Force -Path "$stage/ext/native" | Out-Null
Copy-Item target/release/web.dll "$stage/ext/native/web.dll"
Copy-Item target/release/agent.dll "$stage/ext/native/agent.dll"
Copy-Item target/release/quantum.dll "$stage/ext/native/quantum.dll"
Copy-Item target/release/linalg.dll "$stage/ext/native/linalg.dll"
if (Test-Path "dist/$zip") { Remove-Item "dist/$zip" -Force } if (Test-Path "dist/$zip") { Remove-Item "dist/$zip" -Force }
Compress-Archive -Path "$stage/*" -DestinationPath "dist/$zip" -Force Compress-Archive -Path "$stage/*" -DestinationPath "dist/$zip" -Force
$stageLib = "dist/stage-stdlib" $stageLib = "dist/stage-stdlib"
@@ -85,6 +104,7 @@ jobs:
echo "SOURCE=$source" >> $env:GITHUB_ENV echo "SOURCE=$source" >> $env:GITHUB_ENV
echo "PUBLIC=$public" >> $env:GITHUB_ENV echo "PUBLIC=$public" >> $env:GITHUB_ENV
echo "EXT=$extZip" >> $env:GITHUB_ENV echo "EXT=$extZip" >> $env:GITHUB_ENV
echo "NATIVE=$nativeZip" >> $env:GITHUB_ENV
- name: Upload release assets - name: Upload release assets
uses: softprops/action-gh-release@v2 uses: softprops/action-gh-release@v2
@@ -92,24 +112,21 @@ jobs:
body: | body: |
## Marqdo ${{ github.ref_name }} ## Marqdo ${{ github.ref_name }}
### 亮点 / Highlights — v0.3.4 ### 亮点 / Highlights
- **`ext/web` 定制 C0–C4**:admin 前缀可让出、段边界 gates、shell/layout、样式 strict、脚本 defer/version、条件导航 when/media - **中层标准库 Mid M1–M6**:`re`/`encoding`/`path`/`hash`/`secrets`/`csv`/`text++`/`cli`/`log`/`uuid`(中英对称,零插件)
- **ext/web customization C0–C4**: configurable admin/gates/shell/layout/CSS strict/script lifecycle/conditional nav - **Mid stdlib M1–M6**: regex, encoding, path, hash, secrets, csv, thickened text/lists, cli, log, uuid
- Agent Wave B0 demos + writeback shadow fix - **`ext/linalg` 公式文档面 F1–F3**;agent/llm 写回与 SSE 稳健性修复
- Install: `marqdo ext list` / `marqdo ext add llm|agent|web|quantum` - Details: [CHANGELOG](https://github.com/cflmy/marqdo/blob/main/CHANGELOG.md) · [stdlib-mid.md](https://github.com/cflmy/marqdo/blob/main/doc/design/stdlib-mid.md)
- Native plugins: `cargo build --release -p marqdo_plugin_*` then `ext add`
- Details: [CHANGELOG](https://github.com/cflmy/marqdo/blob/main/CHANGELOG.md) · [README](https://github.com/cflmy/marqdo/blob/main/README.md)
Also: embedded stdlib, writeback/subtask, Variables modal in `view`, VSIX from branch **`vscode-extension`**.
### Downloads ### Downloads
| Asset | Contents | | Asset | Contents |
|-------|----------| |-------|----------|
| `marqdo-*-windows-msvc.exe` | CLI with **embedded** official stdlib | | `marqdo-*-windows-msvc.exe` | CLI with **embedded** official stdlib |
| `marqdo-*-windows-msvc.zip` | `marqdo.exe` + `lib/` (optional disk override) | | `marqdo-*-windows-msvc.zip` | `marqdo.exe` + `lib/` + `ext/`(含 native DLL) |
| `marqdo-*-native-*.zip` | 预编译 `web`/`agent`/`quantum` 插件 |
| `marqdo-*-stdlib.zip` | `lib/` only | | `marqdo-*-stdlib.zip` | `lib/` only |
| `marqdo-*-ext.zip` | Official `ext/` L1 sources (for `MARQDO_EXT_SOURCE` / local `./ext`) | | `marqdo-*-ext.zip` | Official `ext/` L1 sources |
| `marqdo-*-source.zip` | Source snapshot (`git archive`) | | `marqdo-*-source.zip` | Source snapshot (`git archive`) |
| `marqdo-*.vsix` | VS Code / Cursor extension | | `marqdo-*.vsix` | VS Code / Cursor extension |
| `marqdo-*-public.zip` | Static user docs site | | `marqdo-*-public.zip` | Static user docs site |
@@ -120,9 +137,42 @@ jobs:
dist/${{ env.ZIP }} dist/${{ env.ZIP }}
dist/${{ env.STDLIB }} dist/${{ env.STDLIB }}
dist/${{ env.EXT }} dist/${{ env.EXT }}
dist/${{ env.NATIVE }}
dist/${{ env.VSIX }} dist/${{ env.VSIX }}
dist/${{ env.SOURCE }} dist/${{ env.SOURCE }}
dist/${{ env.PUBLIC }} dist/${{ env.PUBLIC }}
fail_on_unmatched_files: true fail_on_unmatched_files: true
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
linux-native:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Build official plugins (release)
run: cargo build --release -p marqdo_plugin_web -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg
- name: Package native zip
run: |
set -euo pipefail
VER="${GITHUB_REF_NAME#v}"
TARGET="x86_64-unknown-linux-gnu"
mkdir -p dist/stage-native/native
cp target/release/libweb.so dist/stage-native/native/libweb.so
cp target/release/libagent.so dist/stage-native/native/libagent.so
cp target/release/libquantum.so dist/stage-native/native/libquantum.so
cp target/release/liblinalg.so dist/stage-native/native/liblinalg.so
(cd dist/stage-native && zip -r "../marqdo-${VER}-native-${TARGET}.zip" .)
echo "NATIVE=marqdo-${VER}-native-${TARGET}.zip" >> "$GITHUB_ENV"
- name: Upload Linux native plugins
uses: softprops/action-gh-release@v2
with:
files: dist/${{ env.NATIVE }}
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+4
View File
@@ -63,8 +63,12 @@ plugins/*/tmp/
# Quantum draw/SVG runtime outputs (sources & gold .mq.md stay tracked) # Quantum draw/SVG runtime outputs (sources & gold .mq.md stay tracked)
/tests/ext/quantum-*.svg /tests/ext/quantum-*.svg
/tests/ext/linalg-*.svg
/examples/quantum-*/**/*.svg /examples/quantum-*/**/*.svg
/examples/quantum-*/*.svg /examples/quantum-*/*.svg
/examples/linalg-*/**/*.svg
/examples/linalg-*/*.svg
/examples/linalg-*.svg
# Local ABI plugin copies (use `marqdo ext add quantum` / cargo build) # Local ABI plugin copies (use `marqdo ext add quantum` / cargo build)
/ext/native/ /ext/native/
+51
View File
@@ -8,6 +8,57 @@
### Changed ### Changed
## v0.3.6 — 2026-09-07
### Highlights
**中层标准库 Mid M1–M6 收口**:通用原语沉入 `lib/`(正则、编解码、路径、哈希、机密、CSV、加厚文本/集合、CLI、日志、UUID),写日常脚本与多数扩展库不再需要原生插件。另含 `ext/linalg` 公式文档面与 agent/llm 稳健性修复。
```bash
git checkout v0.3.6
# 或下载 Release 的 exe / zip
marqdo run public/features/13-log-uuid.mq.md
marqdo ext add linalg # 可选:公式文档 / 线性代数
```
### Added
- **中层标准库 M6**:`lib/log` / `日志`(级别过滤的一行日志)与 `lib/uuid` / `标识`(v4);金样与 public `13-log-uuid`。设计 [stdlib-log-uuid.md](doc/design/stdlib-log-uuid.md)。**Mid M1–M6 收口。**
- **中层标准库 M5**:`table`/`表` 加厚(sort/sort_by/unique/chunk/zip/flatten)与 `lib/cli`/`命令行`(parse);金样与 public `12-list-cli`。设计 [stdlib-list-cli.md](doc/design/stdlib-list-cli.md)。
- **中层标准库 M4**:`lib/csv` / `逗号表`(parse/stringify)与 `lib/text` / `文本` 加厚(contains/replace/case/pad/…);金样与 public `11-csv-text`。设计 [stdlib-csv.md](doc/design/stdlib-csv.md) · [stdlib-text-mid.md](doc/design/stdlib-text-mid.md)。
- **中层标准库 M3**:`lib/hash` / `哈希`(sha256/sha1/md5/hmac_sha256)与 `lib/secrets` / `机密`(token_hex/token_urlsafe);金样与 public `10-hash-secrets`。设计 [stdlib-hash.md](doc/design/stdlib-hash.md) · [stdlib-secrets.md](doc/design/stdlib-secrets.md)。
- **中层标准库 M2**:`lib/path` / `lib/路径`(join/split/normalize/…)与 `fs`/`文件` 加厚(`copy_file`/`move`/`make_temp`);金样与 public `09-path-fs`。设计 [stdlib-path.md](doc/design/stdlib-path.md) · [stdlib-fs-mid.md](doc/design/stdlib-fs-mid.md)。
- **中层标准库 M1**:`lib/re` / `lib/正则`(`is_match`/`find`/`find_all`/`replace`/`split`)与 `lib/encoding` / `lib/编码`(base64/hex);host `host_re_*` + `host_encoding_*`;金样与 public `08-re-encoding`。设计 [stdlib-mid.md](doc/design/stdlib-mid.md) · [stdlib-re.md](doc/design/stdlib-re.md) · [stdlib-encoding.md](doc/design/stdlib-encoding.md)。
- **`ext/linalg`**:L0–L6 + 公式文档面 **F1–F3**(中缀、`declare`、展示/`explicit` 默认化简、`T_ascii`、public「如何写公式文档」);示例 `linalg-transpose` · `linalg-svd` · `linalg-least-squares`。设计:[ext-linalg-formula-doc.md](doc/design/ext-linalg-formula-doc.md)。
### Fixed
- **`ext/agent`**:写回改走 `wb.record`(避免 `writeback` 形参遮蔽模块名),不再直调 `host_*`。
- **`ext/llm`**:`stream_result` 忽略未知 SSE 类型(如 `reasoning`),不再因此退出。
### Changed
## v0.3.5 — 2026-09-04
### Highlights
**`marqdo ext add` 无需本机 Rust**:Release 发布预编译 native 插件;CLI 可自动下载 L1(`*-ext.zip`)与 `*-native-*.zip`。Windows 便携包内含 `ext/native/*.dll`。
```bash
git checkout v0.3.5
# 或下载 Release 的 exe / zip
marqdo ext add web
marqdo ext add agent
marqdo ext add quantum
```
### Added
- **预编译原生插件资产**:`marqdo-*-native-x86_64-pc-windows-msvc.zip`、`marqdo-*-native-x86_64-unknown-linux-gnu.zip`;Windows zip 捆绑 `ext/` + native DLL。
- **`ext add` 下载回退**:本地无 `target/` 时从 GitHub Release 拉取;`MARQDO_EXT_NO_DOWNLOAD=1` / `MARQDO_EXT_VERSION` 可配置。
### Changed
- 文档:[ext-cli.md](doc/design/ext-cli.md)、[08-ext-deploy-coupling](doc/design/ext-web-customization/08-ext-deploy-coupling.md)、README 安装步骤以预编译为默认路径。
- Release workflow:Windows 编插件并上传 native zip;新增 Linux native job。
## v0.3.4 — 2026-09-04 ## v0.3.4 — 2026-09-04
### Highlights ### Highlights
Generated
+48 -5
View File
@@ -463,6 +463,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]] [[package]]
name = "crypto-common" name = "crypto-common"
version = "0.1.7" version = "0.1.7"
@@ -1018,31 +1024,38 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
[[package]] [[package]]
name = "marqdo" name = "marqdo"
version = "0.3.3" version = "0.3.5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"assert_cmd", "assert_cmd",
"chrono", "chrono",
"clap", "clap",
"futures-util", "futures-util",
"getrandom 0.2.17",
"hmac 0.12.1",
"idna_adapter", "idna_adapter",
"include_dir", "include_dir",
"libc", "libc",
"libloading", "libloading",
"md-5 0.10.6",
"predicates", "predicates",
"pulldown-cmark", "pulldown-cmark",
"regex",
"serde_json", "serde_json",
"sha1",
"sha2 0.10.9",
"thiserror 1.0.69", "thiserror 1.0.69",
"tiny_http", "tiny_http",
"tokio", "tokio",
"tokio-tungstenite 0.24.0", "tokio-tungstenite 0.24.0",
"ureq", "ureq",
"zeroize", "zeroize",
"zip",
] ]
[[package]] [[package]]
name = "marqdo-wasm" name = "marqdo-wasm"
version = "0.3.3" version = "0.3.5"
dependencies = [ dependencies = [
"marqdo", "marqdo",
"serde_json", "serde_json",
@@ -1063,6 +1076,14 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "marqdo_plugin_linalg"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
]
[[package]] [[package]]
name = "marqdo_plugin_quantum" name = "marqdo_plugin_quantum"
version = "0.1.0" version = "0.1.0"
@@ -1100,6 +1121,16 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest 0.10.7",
]
[[package]] [[package]]
name = "md-5" name = "md-5"
version = "0.11.0" version = "0.11.0"
@@ -1330,7 +1361,7 @@ dependencies = [
"bytes", "bytes",
"fallible-iterator 0.2.0", "fallible-iterator 0.2.0",
"hmac 0.13.0", "hmac 0.13.0",
"md-5", "md-5 0.11.0",
"memchr", "memchr",
"rand 0.10.2", "rand 0.10.2",
"sha2 0.11.0", "sha2 0.11.0",
@@ -1718,9 +1749,9 @@ dependencies = [
[[package]] [[package]]
name = "sha1" name = "sha1"
version = "0.10.7" version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"cpufeatures 0.2.17", "cpufeatures 0.2.17",
@@ -2562,6 +2593,18 @@ version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
[[package]]
name = "zip"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "760394e246e4c28189f19d488c058bf16f564016aefac5d32bb1f3b51d5e9261"
dependencies = [
"byteorder",
"crc32fast",
"crossbeam-utils",
"flate2",
]
[[package]] [[package]]
name = "zmij" name = "zmij"
version = "1.0.23" version = "1.0.23"
+12 -2
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "marqdo" name = "marqdo"
version = "0.3.4" version = "0.3.6"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
description = "Marqdo — Markdown markers as an interpreted programming language" description = "Marqdo — Markdown markers as an interpreted programming language"
@@ -17,7 +17,7 @@ default = ["native"]
native = ["cli", "view", "net-host", "plugin-host", "exec-host", "fs-host", "tty"] native = ["cli", "view", "net-host", "plugin-host", "exec-host", "fs-host", "tty"]
cli = ["dep:clap"] cli = ["dep:clap"]
view = ["dep:tiny_http"] view = ["dep:tiny_http"]
net-host = ["dep:ureq", "dep:idna_adapter", "dep:zeroize"] net-host = ["dep:ureq", "dep:idna_adapter", "dep:zeroize", "dep:zip"]
plugin-host = ["dep:libloading"] plugin-host = ["dep:libloading"]
exec-host = [] exec-host = []
fs-host = [] fs-host = []
@@ -32,10 +32,19 @@ pulldown-cmark = "0.12"
serde_json = "1" serde_json = "1"
thiserror = "1" thiserror = "1"
tiny_http = { version = "0.12", optional = true } tiny_http = { version = "0.12", optional = true }
sha2 = { version = "=0.10.9", default-features = false }
sha1 = { version = "=0.10.6", default-features = false }
md-5 = { version = "=0.10.6", default-features = false }
hmac = { version = "=0.12.1", default-features = false }
getrandom = { version = "=0.2.17", default-features = false, features = ["std"] }
# Mid M1: Unicode regex for lib/re (no backrefs; RE2-like).
regex = { version = "=1.13.1", default-features = false, features = ["std", "unicode"] }
# Pin for Cargo/rustc 1.81 (newer rustls/idna/zeroize need edition2024). # Pin for Cargo/rustc 1.81 (newer rustls/idna/zeroize need edition2024).
ureq = { version = "=2.9.7", default-features = false, features = ["tls", "gzip"], optional = true } ureq = { version = "=2.9.7", default-features = false, features = ["tls", "gzip"], optional = true }
idna_adapter = { version = "=1.1.0", optional = true } idna_adapter = { version = "=1.1.0", optional = true }
zeroize = { version = "=1.8.1", optional = true } zeroize = { version = "=1.8.1", optional = true }
# Extract GitHub Release ext/native zips (`marqdo ext add` without local Rust).
zip = { version = "=0.6.6", default-features = false, features = ["deflate"], optional = true }
libloading = { version = "=0.8.6", optional = true } libloading = { version = "=0.8.6", optional = true }
include_dir = "0.7" include_dir = "0.7"
libc = { version = "0.2", optional = true } libc = { version = "0.2", optional = true }
@@ -50,6 +59,7 @@ members = [
"plugins/agent", "plugins/agent",
"plugins/web", "plugins/web",
"plugins/quantum", "plugins/quantum",
"plugins/linalg",
"crates/marqdo-wasm", "crates/marqdo-wasm",
] ]
resolver = "2" resolver = "2"
+13 -10
View File
@@ -99,7 +99,7 @@ marqdo catalog public -o .marqdo
--- ---
## 现状(v0.3.4) ## 现状(v0.3.6)
- 映射与解释器:Phase I 树遍历 + 字节码后端可用;金样例在 `tests/` - 映射与解释器:Phase I 树遍历 + 字节码后端可用;金样例在 `tests/`
- **对象**:`#` = 类型/构造,`##`+ = 函数/方法;见 [objects.md](doc/design/objects.md) - **对象**:`#` = 类型/构造,`##`+ = 函数/方法;见 [objects.md](doc/design/objects.md)
@@ -107,25 +107,26 @@ marqdo catalog public -o .marqdo
- **`marqdo debug`**:独立调试页(断点 / 单步 / locals;默认端口 7430;页面 favicon / 品牌使用官方 Logo) - **`marqdo debug`**:独立调试页(断点 / 单步 / locals;默认端口 7430;页面 favicon / 品牌使用官方 Logo)
- **`marqdo catalog` / `sync`**:OKF 风格 YAML + 模块概念页 - **`marqdo catalog` / `sync`**:OKF 风格 YAML + 模块概念页
- **`marqdo version --check`**:与 GitHub 最新 release 对比 - **`marqdo version --check`**:与 GitHub 最新 release 对比
- 标准库:**内置于二进制**(v0.1.2+);磁盘 `lib/` 或 `MARQDO_LIB` 可覆盖。模块含文本、表、**浏览器效应**、文件、系统、时间、JSON、网络、数学、外联、插件、**自写回**、**子任务** - 标准库:**内置于二进制**(v0.1.2+);磁盘 `lib/` 或 `MARQDO_LIB` 可覆盖。模块含文本、表、**浏览器效应**、文件、系统、时间、JSON、网络、数学、外联、插件、**自写回**、**子任务**;**中层 Mid M1–M6**(`re`/`encoding`/`path`/`hash`/`secrets`/`csv`/`cli`/`log`/`uuid` 及中英对称库,见 [stdlib-mid.md](doc/design/stdlib-mid.md))
- **官方扩展库 `ext/`**(**非** stdlib,本版收口): - **官方扩展库 `ext/`**(**非** stdlib,本版收口):
- **`web`**:W0–W7 + P3 + **W8**;**定制 C0–C4**(admin 前缀/gates/`shell_css`/`layout`/样式 strict/脚本 defer·version/条件导航,见 [ext-web-customization](doc/design/ext-web-customization/));示例 [marqdo-blog](examples/marqdo-blog/);生产路径见 [web-asgi-servers-and-marqdo.md](doc/design/web-asgi-servers-and-marqdo.md) - **`web`**:W0–W7 + P3 + **W8**;**定制 C0–C4**(admin 前缀/gates/`shell_css`/`layout`/样式 strict/脚本 defer·version/条件导航,见 [ext-web-customization](doc/design/ext-web-customization/));示例 [marqdo-blog](examples/marqdo-blog/);生产路径见 [web-asgi-servers-and-marqdo.md](doc/design/web-asgi-servers-and-marqdo.md)
- **`quantum`**:Q0–Q7 + Q8a/b 主题 SVG;示例 [quantum-entanglement](examples/quantum-entanglement/) - **`quantum`**:Q0–Q7 + Q8a/b 主题 SVG;示例 [quantum-entanglement](examples/quantum-entanglement/)
- **`linalg`**:L0–L6 + 公式文档面(中缀 / `declare` / 展示化简);示例 [linalg-transpose](examples/linalg-transpose/) · [linalg-svd](examples/linalg-svd/) · [linalg-least-squares](examples/linalg-least-squares/)
- **`agent`**:A1–A4(OKF 复用、过程事件、上下文预算、RAG/MCP fixture);下一波缺口 [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md) - **`agent`**:A1–A4(OKF 复用、过程事件、上下文预算、RAG/MCP fixture);下一波缺口 [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md)
- **`llm`**:OpenAI 兼容对话 - **`llm`**:OpenAI 兼容对话
- 安装:`marqdo ext list` / `add …` / `remove`([ext-cli.md](doc/design/ext-cli.md);默认 `~/.marqdo/ext`)。原生插件先 `cargo build -p marqdo_plugin_*` 再 `ext add` - 安装:`marqdo ext list` / `add …` / `remove`([ext-cli.md](doc/design/ext-cli.md);默认 `~/.marqdo/ext`)。原生插件先 `cargo build -p marqdo_plugin_*` 再 `ext add`
- **原生插件 ABI**:[`include/marqdo_abi.h`](include/marqdo_abi.h) · [ext-abi.md](doc/design/ext-abi.md);`plugins/{demo,agent,web,quantum}` - **原生插件 ABI**:[`include/marqdo_abi.h`](include/marqdo_abi.h) · [ext-abi.md](doc/design/ext-abi.md);`plugins/{demo,agent,web,quantum,linalg}`
- **用户静态站**:`public/` → `view output` → CI 发布 [gh-pages](https://cflmy.github.io/marqdo/) - **用户静态站**:`public/` → `view output` → CI 发布 [gh-pages](https://cflmy.github.io/marqdo/)
- **VS Code 扩展**:分支 **`vscode-extension`**(`main` 不跟踪源码;见 [vscode-extension-commit.md](doc/design/vscode-extension-commit.md));Release 附带 `.vsix` - **VS Code 扩展**:分支 **`vscode-extension`**(`main` 不跟踪源码;见 [vscode-extension-commit.md](doc/design/vscode-extension-commit.md));Release 附带 `.vsix`
- **浏览器 Marqdo(WASM)**:`marqdo wasm build` + 官方 bridge 自启(作者零业务 JS;桥内可含列表/路由/storage/ws/文件/Canvas/音频/Observer/拖放);`lib/browser` + GFM;`web.client_embed`;示例 [browser-hello](examples/browser-hello/) · [browser-app](examples/browser-app/) · [browser-media](examples/browser-media/) · [web-client-site](examples/web-client-site/)([ADR 0002](doc/adr/0002-browser-marqdo-wasm.md) · [D](doc/roadmap/browser-wasm-d.md) · [E](doc/roadmap/browser-wasm-e.md) · [F](doc/roadmap/browser-wasm-f.md)) - **浏览器 Marqdo(WASM)**:`marqdo wasm build` + 官方 bridge 自启(作者零业务 JS;桥内可含列表/路由/storage/ws/文件/Canvas/音频/Observer/拖放);`lib/browser` + GFM;`web.client_embed`;示例 [browser-hello](examples/browser-hello/) · [browser-app](examples/browser-app/) · [browser-media](examples/browser-media/) · [web-client-site](examples/web-client-site/)([ADR 0002](doc/adr/0002-browser-marqdo-wasm.md) · [D](doc/roadmap/browser-wasm-d.md) · [E](doc/roadmap/browser-wasm-e.md) · [F](doc/roadmap/browser-wasm-f.md))
- 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) · [ADR 0002 — 浏览器 WASM](doc/adr/0002-browser-marqdo-wasm.md)(C0–C5 完结,见 [roadmap/browser-wasm.md](doc/roadmap/browser-wasm.md))· [ADR 0003 — 异步效应](doc/adr/0003-browser-async-effects.md) - 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) · [ADR 0002 — 浏览器 WASM](doc/adr/0002-browser-marqdo-wasm.md)(C0–C5 完结,见 [roadmap/browser-wasm.md](doc/roadmap/browser-wasm.md))· [ADR 0003 — 异步效应](doc/adr/0003-browser-async-effects.md)
### 如何使用最新 Marqdo(v0.3.4) ### 如何使用最新 Marqdo(v0.3.6)
```bash ```bash
# 1) 源码安装(跟 tag 或 main) # 1) 源码安装(跟 tag 或 main)
git clone https://github.com/cflmy/marqdo.git && cd marqdo git clone https://github.com/cflmy/marqdo.git && cd marqdo
git checkout v0.3.4 # 或留在 main git checkout v0.3.6 # 或留在 main
cargo build --release cargo build --release
export PATH="$PWD/target/release:$PATH" export PATH="$PWD/target/release:$PATH"
marqdo version marqdo version
@@ -138,12 +139,14 @@ marqdo view public --no-open
marqdo debug public --no-open marqdo debug public --no-open
marqdo catalog public -o .marqdo marqdo catalog public -o .marqdo
# 3) 安装官方扩展(需先编原生插件) # 3) 安装官方扩展(Release 用户:无需本机 Rust)
cargo build --release -p marqdo_plugin_web -p marqdo_plugin_agent -p marqdo_plugin_quantum marqdo ext add web # 或:网页 — 自动下载 L1 + 预编译 native
marqdo ext add web # 或:网页
marqdo ext add agent # 或:智能体 marqdo ext add agent # 或:智能体
marqdo ext add quantum # 或:量子 marqdo ext add quantum # 或:量子
marqdo ext add llm # 或:大模型 marqdo ext add linalg # 或:线性代数
marqdo ext add llm # 或:大模型(纯 .mq.md)
# 开发者也可:cargo build --release -p marqdo_plugin_* 后在本机 target/ 安装
# 离线:解压 Windows zip(已含 ext/native)或手动放下 native zip;MARQDO_EXT_NO_DOWNLOAD=1
# 4) 动态站示例(扩展装好后) # 4) 动态站示例(扩展装好后)
marqdo run examples/marqdo-blog/index.mq.md marqdo run examples/marqdo-blog/index.mq.md
@@ -154,7 +157,7 @@ marqdo wasm build
# → dist/wasm/ … 见 examples/browser-hello/ # → dist/wasm/ … 见 examples/browser-hello/
# 6) Windows:也可从 GitHub Releases 下载 exe / zip / vsix # 6) Windows:也可从 GitHub Releases 下载 exe / zip / vsix
# https://github.com/cflmy/marqdo/releases/tag/v0.3.4 # https://github.com/cflmy/marqdo/releases/tag/v0.3.6
``` ```
开发期也可用 `cargo run -- …` 代替已安装的 `marqdo`: 开发期也可用 `cargo run -- …` 代替已安装的 `marqdo`:
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "marqdo-wasm" name = "marqdo-wasm"
version = "0.3.4" version = "0.3.6"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
description = "Marqdo interpreter as WebAssembly (browser host bridge)" description = "Marqdo interpreter as WebAssembly (browser host bridge)"
+3
View File
@@ -82,6 +82,9 @@ Marqdo 换 **Rust 或 C++** 都不改变「主前端不该是 Flex/Bison」这
| **clap** | CLI | `marqdo run [path]` | | **clap** | CLI | `marqdo run [path]` |
| **serde** + **serde_yaml** | 元信息 | 仅解析 frontmatter 中的 YAML **键值**;`import x:x.mq.md` 仍按**行**抽取(见 Spike) | | **serde** + **serde_yaml** | 元信息 | 仅解析 frontmatter 中的 YAML **键值**;`import x:x.mq.md` 仍按**行**抽取(见 Spike) |
| **ureq** (=2.9.7) | HTTP(S) | `lib/net`;为 Cargo 1.81 另钉 `idna_adapter`/`zeroize`(见根 `Cargo.toml`) | | **ureq** (=2.9.7) | HTTP(S) | `lib/net`;为 Cargo 1.81 另钉 `idna_adapter`/`zeroize`(见根 `Cargo.toml`) |
| **regex** (=1.13.1) | 正则 | Mid M1 `lib/re`;`default-features=false` + `std`/`unicode`;无回溯引用 |
| **sha2** (=0.10.9) / **sha1** (=0.10.6) / **md-5** (=0.10.6) / **hmac** (=0.12.1) | 摘要 | Mid M3 `lib/hash` |
| **getrandom** (=0.2.17) | OS 熵 | Mid M3 `lib/secrets` |
#### 强烈建议 #### 强烈建议
+64 -33
View File
@@ -2,9 +2,9 @@
| | | | | |
|---|---| |---|---|
| Status | **Accepted (v0.1)** | | Status | **Accepted (v0.3.5 prep)** — prebuilt native download |
| Date | 2026-08-07 | | Date | 2026-09-04 |
| Related | [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [ext-abi.md](ext-abi.md) | | Related | [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [ext-abi.md](ext-abi.md) · [08-ext-deploy-coupling.md](ext-web-customization/08-ext-deploy-coupling.md) |
## Commands ## Commands
@@ -12,8 +12,8 @@
marqdo ext list marqdo ext list
marqdo ext add llm marqdo ext add llm
marqdo ext add agent marqdo ext add agent
marqdo ext add web
marqdo ext remove llm marqdo ext remove llm
marqdo ext remove agent
``` ```
## Install root ## Install root
@@ -23,57 +23,88 @@ marqdo ext remove agent
Imports use paths like `import llm:ext/ai/llm.mq.md` (see [`load.rs`](../../src/load.rs)). Imports use paths like `import llm:ext/ai/llm.mq.md` (see [`load.rs`](../../src/load.rs)).
## Source for `add` ## Goal: no local Rust required
End users who install Marqdo from **GitHub Releases** should run:
```bash
marqdo ext add web
```
and get both L1 `.mq.md` **and** the matching native plugin (`.dll` / `.so`) **without** `cargo build`.
### Resolution order (`add` with native crate)
1. Local artifact (`MARQDO_*_PLUGIN`, `target/{debug,release}`, beside `marqdo`, `MARQDO_EXT_SOURCE/native`, …)
2. **Download** `marqdo-{VER}-native-{target}.zip` from the GitHub Release for this CLI version → cache under `~/.marqdo/cache/`
3. If Rust/`cargo` is available → `cargo build -p marqdo_plugin_*` (developers)
4. Else fail with a clear message pointing at the Release assets
L1 sources:
1. `MARQDO_EXT_SOURCE` / repo `./ext` / beside binary
2. Else download `marqdo-{VER}-ext.zip` into `~/.marqdo/cache/ext-src-v{VER}/`
### Env knobs
| Variable | Meaning |
|----------|---------|
| `MARQDO_EXT_VERSION` | Override SemVer used for Release asset names (default: CLI `CARGO_PKG_VERSION`) |
| `MARQDO_EXT_NO_DOWNLOAD=1` | Never hit the network (CI / air-gap); local/cargo only |
| `MARQDO_WEB_PLUGIN` / `MARQDO_AGENT_PLUGIN` / `MARQDO_QUANTUM_PLUGIN` | Explicit native path |
Windows Release **zip** also ships `ext/` + `ext/native/*.dll` next to `marqdo.exe` so a portable unzip works offline after one download.
## Source for `add` (developers)
1. `MARQDO_EXT_SOURCE` — directory containing `ai/llm.mq.md` … 1. `MARQDO_EXT_SOURCE` — directory containing `ai/llm.mq.md` …
2. Else repo `./ext` near cwd / binary 2. Else repo `./ext` near cwd / binary
3. Else Release `marqdo-*-ext.zip` (see above)
## Layout (`ext/`) ## Layout (`ext/`)
Official extensions live under **`ext/ai/`** (LLM + agent framework only). Do not add flat `.mq.md` files at `ext/` root — keeps the tree from accumulating unrelated modules.
```text ```text
ext/ ext/
ai/ ai/ # llm + agent L1
llm.mq.md web/
大模型.mq.md quantum/
agent.mq.md
智能体.mq.md
``` ```
Future optional ext domains get their own subdirs (e.g. `ext/web/`, `ext/quantum/`), not mixed into `ai/`. Installed tree (`~/.marqdo/ext` or `MARQDO_EXT`):
```text
web/web.mq.md
web/网页.mq.md
native/libweb.so # or web.dll / libweb.dylib
web.plugin # absolute path hint
```
## Catalog ## Catalog
| Id | Installs under `MARQDO_EXT` | | Id | Installs under `MARQDO_EXT` |
|----|-----------------------------| |----|-----------------------------|
| `llm` | `ai/llm.mq.md`, `ai/大模型.mq.md` | | `llm` | `ai/llm.mq.md`, `ai/大模型.mq.md` |
| `agent` | `ai/agent.mq.md`, `ai/智能体.mq.md`, **`native/libagent.so`** (or `.dylib`) | | `agent` | `ai/agent.mq.md`, `ai/智能体.mq.md`, **native `agent`** |
| `web` | `web/web.mq.md`, `web/网页.mq.md`, **`native/libweb.so`** — 见 [ext-web.md](ext-web.md) · [roadmap](../roadmap/ext-web.md) | | `web` | `web/web.mq.md`, `web/网页.mq.md`, **native `web`** |
| `quantum` | `quantum/quantum.mq.md`, `quantum/量子.mq.md`, **`native/libquantum.so`** — 见 [ext-quantum.md](ext-quantum.md) · [roadmap](../roadmap/ext-quantum.md) | | `quantum` | `quantum/quantum.mq.md`, `quantum/量子.mq.md`, **native `quantum`** |
`add agent` / `add web` / `add quantum` **先定位原生插件**,再复制 `.mq.md`: ## Release assets (native)
1. 在 `CARGO_TARGET_DIR`、`target/{debug,release}`、源码 `ext` 旁的 `target/`、可执行文件旁等路径查找 `lib{name}.so`(或 `.dll` / `.dylib`) | Asset | Platform |
2. 找不到则自动执行 `cargo build -p marqdo_plugin_{name}`,再安装到 `MARQDO_EXT/native/` |-------|----------|
3. 写入 `{name}.plugin` 绝对路径提示,供 `plugin.native_path` 解析 | `marqdo-VER-native-x86_64-pc-windows-msvc.zip` | Windows x64 (`native/*.dll`) |
| `marqdo-VER-native-x86_64-unknown-linux-gnu.zip` | Linux x64 (`native/lib*.so`) |
这样不会出现「只装了 `.mq.md`、运行时报 native plugin not found」的半安装状态。 macOS: use local `cargo build` until CI adds Darwin natives (same zip naming pattern reserved).
```bash
# 推荐:在仓库根目录
marqdo ext add web
# 等价于找到或编好 libweb.so 后拷入 ~/.marqdo/ext/native/
```
Without a prior install, runtime still resolves via `plugin.native_path name=web` (see [ext-abi.md](ext-abi.md)): `MARQDO_WEB_PLUGIN` → `CARGO_TARGET_DIR/{debug,release}/` → cwd `target/…` → beside `marqdo` binary → `MARQDO_EXT/native/`.
## Tests ## Tests
`tests/gold.rs`: `ext_cli_add_list_remove_llm`, `ext_cli_add_agent`, `ext_agent_framework_smoke`, `ext_agent_run_live` ([`tests/ext/agent-run-live.mq.md`](../../tests/ext/agent-run-live.mq.md); needs `tests/ext/.env` + built `libagent`). `tests/gold.rs`: `ext_cli_add_list_remove_llm`, `ext_cli_add_agent`, `ext_cli_add_web` (local build path; set `MARQDO_EXT_NO_DOWNLOAD=1` in sandboxes if needed).
Unit: `ext_fetch` URL / triple tests.
## Non-goals ## Non-goals
- Third-party registry - Third-party registry
- Merging into `lib/` - Merging domain helpers into `lib/`
- Bundling domain/task helpers in official ext - Guaranteeing plugin **source** ABI stability (author `.mq.md` API is the contract)
+235
View File
@@ -0,0 +1,235 @@
# `ext/linalg` — 公式文档面(跑通即正确)
| | |
|---|---|
| 状态 | **Draft · 锁定作者面哲学与分期** |
| 日期 | 2026-09-07 |
| 父设计 | [ext-linalg.md](ext-linalg.md) |
| 路线图 | [roadmap/ext-linalg.md](../roadmap/ext-linalg.md) |
| 调研 | [research/ext-linalg-formula.md](../research/ext-linalg-formula.md) |
| 对照 | [stdlib-math.md](stdlib-math.md)(高中标量公式) |
---
## 0. 一句话
**`.mq.md` 里的线性代数应当读起来像一份可运行的公式讲义**:式子能跑通,就在库承诺的规则与维数下代表正确;`lib/math` 守高中边界,大学符号矩阵与更深层计算由 **`ext/linalg` 正当承担**。
---
## 1. 产品哲学
### 1.1 与 Marqdo 总原则对齐
| Marqdo | 在线性代数上的含义 |
|--------|-------------------|
| 代码即文档 | 矩阵代数段落用数学式书写,而不是 API 调用清单 |
| 标记即语法 | `$$`、中缀、`# matrix` 方法都是可读标记,不是「库胶水」 |
| 跑通可验收 | 金样 / 课堂作业:文件 exit 0 + 断言化简结果 = 式子合法 |
### 1.2 「跑通即正确」的精确定义
**不是**「任意高深定理自动证明」。
**是**在声明的能力边界内,下列任一成立则文档该段视为正确:
1. **形状层**:不相容维数的乘加在构造时硬错误(写不出可运行的坏式)。
2. **恒等式层**:文档断言的规范形(如 `(AB)^\top → B^\top A^\top`)经规则集得到。
3. **数值核对层**(可选):小 `$$` 数值阵 `explicit` 后与手算一致——验算,不是符号证明。
更高深主题(矩阵微积分、表示、特殊函数矩阵等)通过**扩展同一公式面的规则表**分期加入,而不是另起一套函数库叙事。
### 1.3 与 `lib/math` 的边界(再次锁定)
| | `lib/math` | `ext/linalg` |
|--|------------|--------------|
| 教育边界 | 高中标量公式 | 大学经典线性代数及更深(官方 ext) |
| 值类型 | `Value::Formula`(标量 AST) | `_type=matrix` / `矩阵`(MatExpr + 稠密) |
| 作者感觉 | `$$sin(x)+x$$` + `diff` / `plot` | `A*B`、`(AB)^T`、分块、分解图 |
| 是否「数学库」 | 是(stdlib) | **是数学库的官方扩展**,不是旁路工具箱 |
把大学 LA 塞进 `lib/math` ❌;在 `ext/linalg` 做大学符号计算乃至更深 ✅。
---
## 2. 两面模型:代数面 vs 计算面
文档里应能一眼分清「推导」与「动手算」。
### 2.1 代数面(默认;公式文档)
| 意图 | 写法(目标) | 语义 |
|------|--------------|------|
| 引入符号阵 | 见 §3 | 惰性 `MatrixSymbol` |
| 加减乘 | `` `C` = `A` + `B` `` · `` `P` = `A` * `B` `` | 建树 + 形状检查 |
| 转置 / 逆 | `` `At` = > `A`.T `` · `` `Ai` = > `A`.inv `` | 惰性节点 |
| 化简 / 展示 | 见 §4(默认化简策略) | 规则不动点 → ASCII / KaTeX |
| 分块 / Kronecker | 表或 `block` / `kron`(可保留具名,但结果仍是公式) | 惰性 |
**读者感受**:像在抄黑板,而不是在调 `la.mul`。
### 2.2 计算面(显式步骤;可单独成段)
| 意图 | 写法 | 语义 |
|------|------|------|
| 稠密求值 | `` `E` = > `A`.explicit `` / `la.explicit` | 展开数值 |
| 分解 | `` `f` = > la.factorize matrix=`A` kind="svd" `` | `linalg_factor` |
| 求解 / 最小二乘 | `solve` / `lstsq` | 数值解 |
| 度量 | `norm` / `cond` / `rank` / `det`(数值路径) | 标量或条件对象 |
| 作图 | `draw` / `show` | SVG / 公式卡 |
**读者感受**:「于是我们计算 / 画出…」——与纯代数叙述分段书写。
---
## 3. 目标作者语法(公式文档优先)
### 3.1 已落地(v1 中缀 / 方法)
```markdown
*`A` = > la.symbol name="A" rows=2 cols=2*
*`B` = > la.symbol name="B" rows=2 cols=2*
*`C` = `A` + `B`*
*`P` = `A` * `B`*
*`Pt` = > `P`.T*
*`s` = > `Pt`.simplify*
*`t` = > `s`.ascii*
```
数值 `$$` 矩阵可直接参与 `+` / `*`(经插件吸收为稠密叶)。
### 3.2 目标形态(文档感更强;分期实现)
**叙事理想(不必一次做完):**
```markdown
# 正规方程
令 \(A\) 为 \(m\times n\) 设计矩阵,\(b\) 为观测。
*`AtA` = > `A`.T * `A`* # 或未来更紧凑的 postfix / 混合写法
*`xty` = > `A`.T * `b`*
*`xhat` = > `AtA`.solve b=`xty`* # 计算面
```
**符号引入(减少 `la.symbol` 噪音)——候选(F2 选定一种):**
| 候选 | 例 | 利 | 弊 |
|------|----|----|-----|
| A. 构造糖 | `` `A` = > la.matrix name=A rows=m cols=n `` | 已有 `# matrix` | 仍是调用 |
| B. 维数表 | GFM 表声明符号与形状,一行绑定 | 很「文档」 | 需新约定 |
| C. `$$` 符号方言 | `$$A_{m\times n}$$` → MatExpr | 最像讲义 | 解析歧义;勿污染标量 formula |
| D. 保持 `symbol` + 短别名 | `` `A` = > la.A rows=m cols=n `` | 实现便宜 | 文档感一般 |
**锁定倾向:** 短期 **A + 默认化简(F1)**;**F2 采用 B(GFM `@` 表 + `la.declare`)**;受限 C(`$$` 符号方言)仍待评估且不得污染核心 formula。
### 3.3 明确不做(本波语法)
- 把符号 MatExpr 塞进核心 `formula::Expr`
- 发明与 Markdown 列表冲突的行首 `+` 矩阵语法
- 用未定义的 `$$矩阵A$$` 伪方言冒充已实现语义
---
## 4. 默认化简与「写出即规范」
### 4.1 策略
| 操作 | 是否自动 `simplify` | 理由 |
|------|---------------------|------|
| `ascii` / `latex` / `show` | **是(F1 done)** | 展示面应接近讲义规范形;`raw=True` 可关 |
| 中缀 `+` `*` `-` 的构造结果 | **否** | 保留用户书写结构;需要时再化简 |
| `.simplify` | 显式不动点 | 文档断言 / 金样 |
| `explicit` 前 | 可选先 simplify(F1.1) | 减少无效稠密路径 |
### 4.2 验收句式(金样)
```markdown
*`P` = `A` * `B`*
*`Pt` = > `P`.T*
*`t` = > `Pt`.ascii* # F1:无需再 .simplify 即得 B^T*A^T
1. `t` == "B^T*A^T"
> print text=ok
```
「跑通 + 断言」= 这段公式文档正确。
---
## 5. 正确性分层与错误体验
| 层 | 失败时 | 作者应看到 |
|----|--------|------------|
| 形状 | `A*B` 列行不配 | 含双方 shape 的硬错误,指向调用点 |
| 规则 | 无法化到文档期望形 | 金样失败;或 `ascii` 仍为未化简形(需补规则) |
| 插件未加载 | 中缀矩阵运算 | 提示 `ext add linalg` / `ensure_plugin` |
| 超维 | `explicit` / 分解 | 护栏错误,建议保持符号 |
---
## 6. 与实现现状的对照
| 能力 | 状态 |
|------|------|
| MatExpr 惰性树 + R1–R5 / 分块 / kron | done(L1–L3) |
| 稠密 / 分解 / 度量 / draw theme | done(L2–L6) |
| 中缀 `+` `-` `*` + `# matrix` 方法 | done(v1) |
| `ascii`/`latex`/`show` 默认 simplify | **done(F1)** |
| 更少噪音的符号引入 | **done(F2:`declare` + 表)** |
| 更紧凑转置 / 混合公式书写 | **done(F3:`T_ascii`)** |
| `explicit` 前可选 auto-simplify | **done(F3,默认开)** |
| public「如何写公式文档」 | **done(F3)** |
| 矩阵微积分等 | L7+ / 规则表扩展 |
---
## 7. 分期(公式文档面)
| 阶段 | 内容 | 出口 |
|------|------|------|
| **F1** | 展示路径默认 simplify;金样「`.T` + `ascii` → 规范形」;设计本文落地 | **done** |
| **F2** | 符号引入降噪(选定 §3.2 候选);更新 examples 使其读起来像讲义 | **done**(`declare` + `linalg-transpose`) |
| **F3** | 可选 postfix / 链式糖;`explicit` 前可选 auto-simplify;public 短文「如何写公式文档」 | **done** |
| **F4+** | 新规则(伴随、多项式矩阵…);矩阵微积分仅当明确需要 | 规则表可测 |
---
## 8. 示例叙事(目标文档骨架)
````markdown
---
import la:ext/linalg/linalg.mq.md
---
# 转置反序
*`A` = > la.symbol name="A" rows=2 cols=3*
*`B` = > la.symbol name="B" rows=3 cols=2*
*`P` = `A` * `B`*
*`Pt` = > `P`.T*
叙述:\((AB)^\top = B^\top A^\top\)。
*`t` = > `Pt`.ascii*
1. `t` == "B^T*A^T"
> print text=identity-ok
2. *
> print text=identity-fail
````
计算面示例保持 `examples/linalg-least-squares/`(`lstsq` + heatmap),与代数面示例分目录或分章节。
---
## 9. 开放问题
1. **自动 simplify 是否改变 `ascii` 金样**——凡依赖「未化简乘积串」的测试需改期望或改调 `repr_raw`(若提供)。
2. **中文 `_type=矩阵` 与展示**——方法链已 retag;view 是否统一显示英文 kind。
3. **`$$` 符号方言**——若做,必须经 `from_formula` 扩展,禁止改核心高中 formula 语义。
---
## 10. 收束
`ext/linalg` 的北极星不是「功能最多的矩阵库」,而是:
> **让大学线性代数(及更深)以可运行公式文档的形式存在于 Marqdo;跑通即在规则集内正确;计算与作图是显式步骤,不淹没代数叙述。**
+345
View File
@@ -0,0 +1,345 @@
# `ext/linalg` — 公式优先的线性代数扩展
| | |
|---|---|
| 状态 | **Draft · L0–L6 + 中缀 v1 · 公式文档面见专文** |
| 日期 | 2026-09-07 |
| 调研 | [research/ext-linalg-formula.md](../research/ext-linalg-formula.md) |
| **公式文档面** | [ext-linalg-formula-doc.md](ext-linalg-formula-doc.md)(跑通即正确 · 代数面/计算面) |
| 路线图 | [roadmap/ext-linalg.md](../roadmap/ext-linalg.md) |
| 相关 | [stdlib-math.md](stdlib-math.md) · [ext-abi.md](ext-abi.md) · [ext-cli.md](ext-cli.md) · [view.md](view.md) · [ext-quantum-q7.md](ext-quantum-q7.md) |
| 安装(规划) | `marqdo ext add linalg`(`linalg` / `线性代数`) |
---
## 0. 一句话
**经典线性代数以「矩阵公式」为一等公民**:抽象表达式可化简、可展示、可选求值;稠密数值与分解图走 ABI 插件——**不**并入 `lib/math`,**不**挂在 `ext/quantum` 作者面。
作者面北极星见 **[ext-linalg-formula-doc.md](ext-linalg-formula-doc.md)**:跑通即正确;代数面像讲义,计算面是显式步骤。
---
## 1. 目标与非目标
### 1.1 目标
1. **公式类扩展**:在现有标量 `formula` 之上,引入 **矩阵表达式(MatExpr)**——`MatrixSymbol`、乘加、转置、逆、迹、行列式、分块、Kronecker——默认**不展开**。
2. **大规模优化**:形状检查 + 重写规则 + 惰性求值 + 分解对象复用(见 §5)。
3. **易于展示**:KaTeX 公式卡、结构 SVG(SVD/QR/消元/特征)、数值热力图;CLI 可断言的 ASCII。
4. **文档即实验**:GFM 表声明矩阵 / 步骤;`.mq.md` 可读可跑。
### 1.2 非目标(首版)
| 不做 | 原因 |
|------|------|
| 塞进 `lib/math` | 高中标量范围已锁定;大学 LA 属官方 ext |
| 复用 `quantum.*` 作者 API | 领域混淆;量子 Q7 保持量子语义 |
| 完整 SymPy / Mathematica | 规则表可控子集即可 |
| 稀疏格式 / GPU / 分布式 | 超出文档语言定位 |
| 矩阵微积分闭式(Jacobian 积木) | 记入路线图远期 |
| 在 `ext/**` 调 `host_*` | 硬约束;数值走插件名 |
---
## 2. 分层架构
```text
作者 .mq.md
│ import la:ext/linalg/linalg.mq.md
▼
L1 官方 ext(GFM + ## 包装,中英分文件)
│ plugin.load name=linalg
▼
plugins/linalg(ABI v2)
├── matexpr:符号树构造 / 重写 / latex
├── dense:稠密 f64(可选复数)运算与分解
└── draw:结构 SVG / heatmap
│
▼(可选)
核心 Value::Formula 仅保留「数值矩阵字面量」现状;
符号 MatExpr 以 **map `_type=matrix`(中文构造为 `矩阵`)** 承载,避免撑爆高中 formula AST;遗留标签 `linalg_expr` / `linalg_dense` 仍可解析。
```
### 2.1 为何 MatExpr 不直接塞进 `formula::Expr`
| 方案 | 利 | 弊 | 决策 |
|------|----|----|------|
| 扩展 `formula::Expr` | 与 `$$` 一体 | 核心膨胀;高中/大学边界糊 | ❌ |
| 插件侧表达式 + L1 对象 | 核心干净;可独立发版 | 需稳定 `_type` 与 latex 桥 | ✅ **采用** |
| 仅 list-of-list 数值 | 简单 | 无法公式优化与符号展示 | ❌ 仅作 `explicit` 结果 |
**既有** `formula::Expr::Matrix`(数值 `$$`)继续服务量子门 / 小例子;`ext/linalg` 通过 `la.matrix` / `la.from_formula` 吸收为稠密叶子。
### 2.2 与量子的边界
| | `ext/linalg` | `ext/quantum` Q7 |
|--|--------------|------------------|
| 域 | 经典实/复矩阵 | 态 / 密度 / Pauli |
| Kronecker | 经典 ⊗ | 复合量子系统 |
| 图 | SVD/QR/GE/heatmap | hinton/city/qsphere/… |
| 复用 | 插件**私有**可共享 Jacobi/SVD 代码 | 作者面 API **分离** |
---
## 3. 运行时对象(锁定草案)
全部经插件构造,L1 返回 **map**(`_type` 稳定,金样可断言):
| `_type`(英) | 中文标签 | 字段(最小) | 含义 |
|---------------|----------|--------------|------|
| `linalg_expr` | `线性式` | `repr`(S-expr 或内部 id)、`shape`=`[m,n]` 或符号维、`latex`、`ascii` | 抽象矩阵公式 |
| `linalg_dense` | `稠密阵` | `rows`, `cols`, `data`(行主 list[list])、`dtype`=`real`\|`complex` | 显式数值 |
| `linalg_factor` | `分解` | `kind`=`lu`\|`qr`\|`svd`\|`eig`\|`chol`,因子字段,`latex`,可选 `svg` | 分解结果 |
| `linalg_svg` | — | `kind`, `svg` 文本 | 与 quantum_svg 同构展示约定 |
| `linalg_solve` | `求解` | `x`, `residual`, `method` | 线性系 / 最小二乘 |
形状:`shape` 允许整数或符号维名(字符串 `"n"`);不相容 → 硬错误。
---
## 4. 作者面 API(英文 `ext/linalg/linalg.mq.md`)
中文镜像:`ext/linalg/线性代数.mq.md`(`矩阵` / `化简` / `求值` / `绘图`…)。
### 4.1 构造
```markdown
# matrix
## symbol
+ `name`
+ `rows`
+ `cols`
# → linalg_expr MatrixSymbol
## eye / zeros / ones / diag
+ …
## from_list
+ `data` # list[list] 或 table
## from_formula
+ `formula` # 吸收 Value::Formula Matrix 字面量
## block
+ `blocks` # 表或嵌套 list of expr
```
文档写法偏好:
````markdown
`A` = > la.symbol name=A rows=n cols=n
`b` =
$$
\begin{pmatrix}1\\0\\0\end{pmatrix}
$$
*`b` = > la.from_formula formula=`b` *
````
作者面优先顶层 `la.symbol` / `la.mul`(`la.matrix.symbol` 无接收者会硬错误,见 [module-namespace.md](module-namespace.md) §4.3)。
### 4.2 公式代数(返回 `linalg_expr`,惰性)
| 英文 | 中文 | 语义 |
|------|------|------|
| `mul` / `add` / `sub` | `乘` / `加` / `减` | 形状检查后建树 |
| `transpose` / `T` | `转置` | |
| `inv` | `逆` | 符号逆;不立即求数值 |
| `pow` | `幂` | 整数幂 |
| `trace` / `det` | `迹` / `行列式` | 可得到标量 expr 或数值 |
| `kron` | `克罗内克` | **禁止**默认 expand |
| `simplify` | `化简` | 应用 §5 规则集 |
| `collapse` | `分块化简` | 对标 `block_collapse` |
| `latex` / `ascii` | `乳胶` / `文本` | 展示字符串 |
| `shape` | `形状` | |
链式 / 中缀(v1 已落地):
```markdown
*`C` = `A` + `B`*
*`P` = `A` * `B`*
*`Pt` = > `P`.T*
*`s` = > `Pt`.simplify*
```
运行时对双方为 `matrix` / `矩阵` / 数值 `$$` 矩阵 / 遗留 `linalg_expr` 的 `+` `-` `*` 分派到插件;结果保留左操作数 `_type` 以便中英方法链。
### 4.3 求值与分解(热路径 → 稠密)
| 英文 | 说明 |
|------|------|
| `eval` / `explicit` | 符号树 + 绑定表 → `linalg_dense`;超维硬错误 |
| `factorize` | `kind=lu\|qr\|svd\|eig\|chol` → `linalg_factor` |
| `solve` | `A`,`b`;或 `factor` + `b` |
| `lstsq` | 最小二乘 |
| `rank` / `norm` / `cond` | 数值度量 |
| `matmul` | 稠密乘(已 explicit 时) |
维数上限(草案,可配置):
| 操作 | 默认上限 |
|------|----------|
| `explicit` 稠密元素总数 | \(n\cdot m \le 10^4\)(如 100×100) |
| SVD / eig 方阵边长 | \(\le 64\)(与 quantum 密度同级) |
| 结构 SVG(无元素) | 边长示意可达数百(只画框) |
| KaTeX 完整数字阵 | \(\le 8\times 8\);更大只出摘要 + 结构图 |
### 4.4 展示
| 英文 | `kind` | 说明 |
|------|--------|------|
| `show` | — | 写入 view:优先 `latex` 公式卡 |
| `draw` | `structure` | 形状/分块矩形(drawmatrix 风格) |
| `draw` | `heatmap` / `hinton` | 数值幅度 |
| `draw` | `svd` / `qr` / `ge` / `eig` | 分解结构图(对标 LAFigureSpecs) |
| `draw` | `path`= | 可选落盘 SVG |
`show` 应对符号式**始终可用**(无需 `eval`)。
### 4.5 最小可运行示例(设计验收叙事)
````markdown
---
import la:ext/linalg/linalg.mq.md
import table:lib/table.mq.md
---
# main
*`A` = > la.symbol name=A rows=3 cols=3 *
*`x` = > la.symbol name=x rows=3 cols=1 *
*`b` = > la.mul a=`A` b=`x` *
*`s` = > la.simplify expr=`b` *
> la.show expr=`s`
`M` =
$$
\begin{bmatrix}2&0\\0&3\end{bmatrix}
$$
*`M` = > la.from_list data=[[2,0],[0,3]] *
*`f` = > la.factorize matrix=`M` kind=eig *
> la.draw factor=`f` kind=eig path=eig.svg
````
---
## 5. 公式重写与「大规模优化」(核心)
### 5.1 原则
1. **构造便宜,展开昂贵**——乘加只分配节点。
2. **形状是类型系统**——`(m×k)·(k×n)` 合法;否则错误指向调用点。
3. **化简是规则不动点**——有限规则表,可测、可文档化。
4. **expand 显式开启**——`expand=True` 才对 Kronecker / 多项式矩阵做爆炸性展开。
5. **数值路径旁路符号**——已 `explicit` 的叶子用稠密核,不再走规则。
### 5.2 v1 规则集(最小够用)
| ID | 规则 | 例 |
|----|------|-----|
| R1 | 单位元 | `I*A → A`,`A*I → A`,`A+0 → A` |
| R2 | 双逆 | `(A^{-1})^{-1} → A` |
| R3 | 转置反序 | `(AB)^\top → B^\top A^\top`,`(A^\top)^\top → A` |
| R4 | 逆转置 | `(A^{-1})^\top → (A^\top)^{-1}` |
| R5 | 结合扁平 | `MatMul` 链扁平化 |
| R6 | 分块塌缩 | 对标 `block_collapse`(乘/加在块级分配) |
| R7 | Kronecker 结合 | `(A⊗B)⊗C → A⊗B⊗C`;**不**展开成元素 |
| R8 | 标量提出 | `c*(A+B) → c*A+c*B`(可选;默认关以免树变宽) |
### 5.3 数值侧优化
| 策略 | 说明 |
|------|------|
| `factorize` 一次 | 多 RHS `solve` 复用 |
| 小阵专用核 | 2×2 / 3×3 闭式逆 / det(教学友好、可对照公式) |
| 复杂度护栏 | 超限错误信息建议「保持符号 / 降维 / 分块」 |
| 禁止静默降精度 | 病态 `cond` 可警告(stderr 或结果字段) |
---
## 6. 展示设计(效果)
### 6.1 通道
| 通道 | 符号 MatExpr | 稠密 | 分解 |
|------|--------------|------|------|
| view Structure | KaTeX `latex` 卡 | 小阵 KaTeX;大阵摘要 | 公式 + 可选 SVG |
| view Execution plots | — | heatmap SVG | `kind=svd|…` SVG |
| CLI `print` | `ascii` | `[[…],…]` 或摘要 | `svd(U,S,V)` 摘要 |
| writeback | 可选记录 latex | 可选 | 可选 |
### 6.2 LaTeX 约定
- 符号:`A`,`A^{\top}`,`A^{-1}`,`AB`,`A \otimes B`
- 数值小阵:`\begin{bmatrix}…\end{bmatrix}`
- 分块:`\begin{bmatrix} A & B \\ C & D \end{bmatrix}`
- 喂给既有 view:与 `formula-card` / KaTeX 同源路径(L1 `show` → `host_query("record_plot")` 或扩展「record_math」若已有;无则复用 writeback + plot 列表)
### 6.3 结构 SVG(教学主视觉)
对标 LAFigureSpecs / drawmatrix,**不**依赖 Matplotlib:
| `kind` | 画什么 |
|--------|--------|
| `structure` | 矩形块 + 标签(零块灰色) |
| `svd` | \(U\Sigma V^{\top}\) 块布局 + 秩分隔 |
| `qr` | 瘦/胖 QR 示意 |
| `ge` | 消元后上三角高亮 |
| `eig` | \(A=PDP^{-1}\) 或谱列表 + 对角 |
| `heatmap` | 单元填色(实数蓝红;复数幅值) |
主题:`theme=light|dark|bw`(与 quantum Q8 令牌对齐;默认 `light`)。
---
## 7. L1 / 插件 / CLI 约定
1. **`ext/linalg/**` 禁止 `host_*`**;只调 `plugin.*` 注册名(如 `linalg_simplify`)。
2. **中英分文件**,CATALOG 双 id:`linalg` / `线性代数`。
3. **`marqdo ext add linalg`**:与 web/agent/quantum 相同——优先 Release 预编译 native zip。
4. **金样**:离线、确定性;SVG 断言含关键子串或稳定 hash 前缀。
5. **依赖**:插件内自研稠密核(或慎重引入单一 crate,如 `nalgebra`——**实现期再议**;设计默认「可自研小维 + 可选 crate」)。
---
## 8. 分期(摘要;细节见路线图)
| 阶段 | 交付 |
|------|------|
| **L0** | CATALOG + ping + L1 `ensure_plugin` |
| **L1** | `matrix.symbol` / dense 构造 / `mul`·`T`·`inv` 树 + `simplify` R1–R5 + `latex`/`show` |
| **L2** | `explicit` / 小阵运算 / `solve` / `det`·`trace` 数值 |
| **L3** | 分块 + `collapse`;Kronecker 惰性 |
| **L4** | `factorize` LU/QR/SVD/eig + 结构 SVG |
| **L5** | heatmap/hinton;examples + public 文档 + skill 摘要 |
| **L6+** | 矩阵微积分 / 更大 BLAS(仅当用户明确要求);复数全路径算子 |
---
## 9. 验收标准(设计级)
1. 符号式 `(AB)^\top` 经 `simplify` 得 `B^\top A^\top`,**不**产生稠密数据。
2. 不相容形状在符号 `mul` 即失败。
3. `$$` 数值阵 → `from_formula` → `factorize kind=eig` → view 含 SVG 或 latex。
4. Kronecker 默认不 expand;显式 `expand` 受维数护栏。
5. 中英 L1 金样各至少一条;`ext add linalg` 路径文档化。
6. 与 `ext/quantum` 无作者面符号冲突。
---
## 10. 风险与开放问题
| 项 | 备注 |
|----|------|
| MatExpr 序列化 | 金样 / write:倾向可打印 S-expr 或稳定 latex+shape,避免不透明指针跨进程 |
| 与标量 `formula` 混运算 | v1:标量只作 `c*A` 系数;`det` 结果若符号则另型或降为 `formula` 文本 |
| nalgebra 等依赖 | 实现 L2 时评估二进制体积与 ABI 体积 |
| record_math vs plot | 若仅 SVG/KaTeX 字符串,可先复用 plot/writeback |
---
## 11. 一句话(收束)
**`ext/linalg` 让线性代数公式保持可优化的表达式,用规则与结构图服务文档阅读,用受控稠密核服务验算——公式优先,数值可选,展示分层。**
+1 -1
View File
@@ -210,7 +210,7 @@
- >6 qubit 稠密 ρ 的谱 / Hinton(请用态向量 API) - >6 qubit 稠密 ρ 的谱 / Hinton(请用态向量 API)
- Matplotlib / WebGL / 交互拖拽 - Matplotlib / WebGL / 交互拖拽
- 量子线性系统算法(HHL)、QSVT 电路构造 - 量子线性系统算法(HHL)、QSVT 电路构造
- 把线性代数并入 `lib/math` - 把线性代数并入 `lib/math`(经典公式优先的 LA 见规划 [ext-linalg.md](ext-linalg.md),作者面与 quantum 分离)
--- ---
@@ -55,6 +55,14 @@
--- ---
## 6. 过渡期 workaround ## 6. 过渡期 → 预编译路径(进行中)
锁定 Marqdo 与插件同版本镜像;本地 `MARQDO_EXT` 指向仓库构建产物;定制需求提 issue 对照本目录解决文,避免沉默 fork 漂移。 **目标:** 用户 `marqdo ext add web` **不必**本机 Rust。
| 交付 | 说明 |
|------|------|
| Release `marqdo-*-native-*.zip` | Windows / Linux 预编译 `web`/`agent`/`quantum` |
| Windows portable zip | 内含 `ext/` + `ext/native/*.dll` |
| `ext add` 下载回退 | 本地无 `.so`/`.dll` 时从 GitHub Release 拉取(`MARQDO_EXT_NO_DOWNLOAD=1` 可关) |
仍保留开发者 `cargo build -p marqdo_plugin_*` 路径。详见 [ext-cli.md](../ext-cli.md)。
+29
View File
@@ -0,0 +1,29 @@
# `lib/csv` — 逗号表(Mid M4)
| | |
|---|---|
| 状态 | **已落地(M4)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-text-mid.md](stdlib-text-mid.md) · [stdlib-mid.md](stdlib-mid.md) |
| 导入 | `lib/csv.mq.md` · `lib/逗号表.mq.md` |
| Host | `host_csv_*`(RFC4180 最小集,无第三方 crate) |
---
## 1. API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `parse` | `解析` | `text` | **list of maps**:首行为表头 |
| `stringify` | `序列化` | `rows` | CSV 文本(表头 = 首行 map 的键顺序) |
## 2. 方言(v1)
- 分隔符 `,`;引号 `"`;`""` 转义。
- 换行 `\n` / `\r\n`;忽略末尾空行。
- 字段内可含逗号与换行(须加引号)。
- **不**支持自定义分隔符 / 无表头模式(可后续加)。
## 3. 刻意不做
流式大文件、类型推断、Excel 方言。
+32
View File
@@ -0,0 +1,32 @@
# `lib/encoding` — 编解码(Mid M1)
| | |
|---|---|
| 状态 | **已落地(M1)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [stdlib-re.md](stdlib-re.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
| 导入 | `lib/encoding.mq.md` · `lib/编码.mq.md` |
| Host | `host_encoding_*`(无第三方 crate:标准 Base64 / hex) |
---
## 1. API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `base64_encode` | `基64编码` | `text` | 文本(UTF-8 字节 → 标准 Base64,含 `=` 填充) |
| `base64_decode` | `基64解码` | `text` | 文本(解码后须为合法 UTF-8) |
| `hex_encode` | `十六进制编码` | `text` | 小写 hex |
| `hex_decode` | `十六进制解码` | `text` | 文本(偶数字节;合法 UTF-8) |
## 2. 与 `net.url_encode` 的边界
| 能力 | 归属 |
|------|------|
| `application/x-www-form-urlencoded` 风格 | `net.url_encode` |
| Base64 / hex | **本库** |
| 百分号编解码通用版 | 可日后加 `encoding.percent_*`;本波不做 |
## 3. 刻意不做(v1)
URL-safe Base64 变体开关、忽略空白的宽松解码、二进制 `bytes` 类型(仍经 UTF-8 文本往返)。
+24
View File
@@ -0,0 +1,24 @@
# `lib/fs` 加厚 — 复制 / 移动 / 临时文件(Mid M2)
| | |
|---|---|
| 状态 | **已落地(M2)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-path.md](stdlib-path.md) · [stdlib-mid.md](stdlib-mid.md) |
| 边界 | **文本与普通文件**;不引入 `bytes` 类型(`read_bytes` 延后) |
---
## 1. 新增 API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `copy_file` | `复制文件` | `src`, `dest` | `None`(覆盖已存在的目标文件) |
| `move` | `移动` | `src`, `dest` | `None`(rename;跨设备失败则报错) |
| `make_temp` | `建临时` | 可选 `prefix` | 相对 `cwd`/`fs_root` 的新空文件路径文本 |
均走现有 `resolve_path` 沙箱;写操作受 `fs_write` 策略约束。
## 2. 刻意不做
目录树 `copy_tree`、原子替换、`read_bytes`、权限 chmod。
+26
View File
@@ -0,0 +1,26 @@
# `lib/hash` — 摘要与 HMAC(Mid M3)
| | |
|---|---|
| 状态 | **已落地(M3)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [stdlib-secrets.md](stdlib-secrets.md) |
| 导入 | `lib/hash.mq.md` · `lib/哈希.mq.md` |
| Host | `host_hash_*`(`sha2` / `sha1` / `md-5` / `hmac`) |
---
## 1. API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `sha256` | `哈希256` | `text` | 小写 hex |
| `sha1` | `哈希1` | `text` | 小写 hex |
| `md5` | `md5` | `text` | 小写 hex |
| `hmac_sha256` | `密钥哈希256` | `key`, `text` | 小写 hex |
输入按 **UTF-8 字节**摘要。文档标明:用于完整性标识 / 缓存键,**不是**密码存储(无 Argon2/bcrypt)。
## 2. 刻意不做
密码哈希、流式大文件哈希 API、非 UTF-8 二进制类型。
+36
View File
@@ -0,0 +1,36 @@
# `lib/table` 集合加厚 + `lib/cli`(Mid M5)
| | |
|---|---|
| 状态 | **已落地(M5)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
| 集合 | 加厚现有 `lib/table` / `lib/表`(不另开 `listx`) |
| CLI | `lib/cli.mq.md` / `lib/命令行.mq.md` |
---
## 1. 集合新增
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `sort` | `排序` | `list` | 升序列表(int/num/text 优先;其它按显示文本) |
| `sort_by` | `按键排序` | `list`, `key` | 对 **map 列表**按键值排序 |
| `unique` | `去重` | `list` | 保序去重 |
| `chunk` | `分块` | `list`, `size` | 列表的列表 |
| `zip` | `拉链` | `a`, `b` | `[[a0,b0], …]`(取较短长度) |
| `flatten` | `展平` | `list` | 一层展平 |
`reverse` 已有。
## 2. CLI
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `parse` | `解析` | 可选 `args` | map:`--k v` / `--k=v` → 文本;裸 `--flag` → `True`;位置参 → 键 `_` 列表 |
`args` 缺省时用 `sys.args`(经宿主 `argv`)。
## 3. 刻意不做
稳定自定义比较器、惰性迭代器、完整 argparse 互斥组。
+30
View File
@@ -0,0 +1,30 @@
# `lib/log` + `lib/uuid`(Mid M6)
| | |
|---|---|
| 状态 | **已落地** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
---
## 1. 日志 `lib/log` / `lib/日志`
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `set_level` | `设级别` | `level`(`debug`/`info`/`warn`/`error`) | `None` |
| `debug` / `info` / `warn` / `error` | `调试` / `信息` / `警告` / `错误` | `text` | 达级别则 `print` 一行 `LEVEL message`;否则无输出 |
默认级别:`info`(隐藏 `debug`)。中文级别别名:`调试`/`信息`/`警告`/`错误`。
实现:宿主记最小级别;`host_log_line` 返回待打印文本或 `None`;L1 再 `print`(接入现有 stdout / capture)。
## 2. UUID `lib/uuid` / `lib/标识`
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `v4` | `版本4` | (无) | 标准 8-4-4-4-12 小写 hex(`getrandom`) |
## 3. 刻意不做
结构化 JSON 日志、远程 sink、UUID v1/v7、yaml。
+3 -2
View File
@@ -4,7 +4,7 @@
|---|---| |---|---|
| 状态 | **已实现(M1–M4)** | | 状态 | **已实现(M1–M4)** |
| 日期 | 2026-08-05 | | 日期 | 2026-08-05 |
| 相关 | [stdlib-modules.md](stdlib-modules.md) · [stdlib-i18n.md](stdlib-i18n.md) · [markdown-mapping.md](markdown-mapping.md) · [view.md](view.md) | | 相关 | [stdlib-modules.md](stdlib-modules.md) · [stdlib-i18n.md](stdlib-i18n.md) · [markdown-mapping.md](markdown-mapping.md) · [view.md](view.md) · 大学线性代数见规划 [ext-linalg.md](ext-linalg.md) |
| 导入 | `lib/math.mq.md`(英)· `lib/数学.mq.md`(中) | | 导入 | `lib/math.mq.md`(英)· `lib/数学.mq.md`(中) |
| 范围 | **高中数学**(初等函数、导数、圆锥曲线、简单方程);非大学 CAS | | 范围 | **高中数学**(初等函数、导数、圆锥曲线、简单方程);非大学 CAS |
@@ -92,7 +92,7 @@ $$
| 代入求值 | `subs` → `num` 或公式 | | 代入求值 | `subs` → `num` 或公式 |
| 求解 | 一元一次 / 二次闭式;其它用数值求根并标明 | | 求解 | 一元一次 / 二次闭式;其它用数值求根并标明 |
**不做(本波):** 多元方程组、极限 ε-δ、级数、积分保证、矩阵、复数完备、任意证明。 **不做(本波):** 多元方程组、极限 ε-δ、级数、积分保证、**大学线性代数**(符号矩阵表达式 / 分解 —— 见 [ext-linalg.md](ext-linalg.md))、复数完备、任意证明。数值小矩阵字面量可在 `$$` 中解析为 `formula::Matrix`,但符号 MatExpr 与分解不在 `lib/math`。
### 3.2 作图(P) ### 3.2 作图(P)
@@ -210,6 +210,7 @@ Interpreter.plots: Vec<Stringimport math:lib/math.mq.md · lib/数学.mq.md
- `$$:name` 模块级具名绑定(已废除)。 - `$$:name` 模块级具名绑定(已废除)。
- 无名 `$$` 自动进运行时。 - 无名 `$$` 自动进运行时。
- 大学级数 / 完备积分 / 定理证明。 - 大学级数 / 完备积分 / 定理证明。
- **大学线性代数扩展**(见 [ext-linalg.md](ext-linalg.md);不在本库做符号 MatExpr)。
- 通用隐函数作图引擎。 - 通用隐函数作图引擎。
- 3D、交互式图表库。 - 3D、交互式图表库。
+170
View File
@@ -0,0 +1,170 @@
# 中层标准库(Mid Stdlib)
| | |
|---|---|
| 状态 | **M1–M6 已落地** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib.md](stdlib.md) · [stdlib-modules.md](stdlib-modules.md) · [ext-abi.md](ext-abi.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
| 目标 | 在接下来数个发版周期内,把「通用编程」能力沉到 **L0.5 宿主 + L1 `.mq.md`**,减少「凡是稍有能力就必须 ABI 插件」的路径依赖 |
---
## 1. 为什么现在做
现状分层(已落地):
```text
L0 内置 print / len / split / …
L0.5 宿主原语 src/host/*(供 L1 包装,作者原则上不直调)
L1 官方库 lib/*.mq.md(嵌入二进制 / 可覆盖)
ext 官方扩展 .mq.md + 可选 cdylib(web / agent / quantum / linalg)
```
痛点:
1. **通用语言能力缺口**:`lib/text` 几乎只是 `trim/split/join`;无正则、无编解码、无哈希、无路径代数、无 CSV……日常脚本要么外联 Python,要么新开插件。
2. **插件路径过重**:ABI 插件适合**领域热路径**(量子核、公式矩阵、HTTP 服务+SQLite),不适合「再来一个 base64」。每加一个插件都要:crate、Release 资产、`ext add`、CI、WASM 缺口。
3. **扩展作者被逼进插件**:`ext/**` 禁止 `host_*`。若 L1 没有正则 / 哈希 / 路径,扩展只能 `plugin.load`,导致「库」与「原生模块」边界模糊。
4. **对照成熟语言**:Go 的 `strings` / `regexp` / `encoding/*` / `crypto` / `path/filepath`、Python 的「难写对才进 stdlib」标准——都把**通用原语**留在语言自带层,把**领域产品**留给第三方。Marqdo 的「通用」应对齐前者;`ext/` 对齐后者。
中层标准库 = **在现有 L1 之上、在官方 ext 之下**,专门补「写通用程序必备、又不该做成插件」的那一档。
---
## 2. 分层命名(本波约定)
| 名称 | 位置 | 用户导入 | 实现形态 |
|------|------|----------|----------|
| **核心 L1** | 已有 `lib/{fs,sys,time,json,net,math,…}` | 是 | 薄包装 → 已有 `host_*` |
| **中层 L1(Mid)** | 新增 / 加厚 `lib/{re,encoding,hash,path,…}` | 是(仍属 `lib/` / `std/`) | **优先**薄 `host_*` + L1;能纯 `.mq.md` 则纯库 |
| **官方 ext** | `ext/{web,agent,quantum,linalg,ai}` | 显式 `ext/` | `.mq.md`;**仅**领域热路径允许 ABI 插件 |
| **catalog / 用户库** | 发版外收录 | 各自约定 | 应能主要靠 Mid + 核心 L1 写成,**默认无插件** |
> **不**新开第三棵目录树。中层仍落在 `lib/`,用文档与路线图分期交付,避免 `lib/` vs `lib-mid/` 分裂。
---
## 3. 准入 / 不准入(宪法)
### 3.1 进入中层的条件(须同时满足多数)
| # | 条件 | 说明 |
|---|------|------|
| 1 | **跨领域高频** | 脚本、站点、智能体、教学示例都会用到 |
| 2 | **难写对或不可移植** | 正则、Unicode、密码学、路径跨平台——纯 Marqdo 成本过高 |
| 3 | **解锁「无插件写库」** | 有了它,更多 `ext/` / catalog 可用纯 `.mq.md` |
| 4 | **宿主面积极小** | 一个或少数 `host_*` + 稳定 crate;不把巨型框架链进主二进制 |
| 5 | **API 可长期冻住** | 宁可窄接口,也不提前做成第二个 Requests |
参考(外部):
- Python:[stdlib 准入](https://devguide.python.org/developer-workflow/stdlib/) 与 [Language Summit:stdlib 何用](https://blog.python.org/2023/05/the-python-language-summit-2023-what-is.html)——偏「启动器 / 新手脚本 / 难写对的协议」,而非「万物入 std」。
- Go:日常几乎离不开的 `strings`、`regexp`、`encoding/*`、`crypto`、`path/filepath`、`slices`——对标我们的 Mid 候选,而非 `database/sql` 驱动全家桶。
- 反面:PEP 594「死电池」——过窄协议、过时格式、与生态脱节的模块不要进 Mid。
### 3.2 明确不准入(继续走 ext + 可选插件)
| 领域 | 原因 |
|------|------|
| HTTP **服务端** / ASGI / 路由框架 | 产品面;已有 `ext/web` |
| SQLite / ORM / 管理后台 | 同上 |
| LLM / 智能体编排 / OKF | `ext/ai` · `ext/agent` |
| 量子线路 / 密度矩阵核 | `ext/quantum` |
| 大学线性代数 CAS / 分解可视化 | `ext/linalg`(公式文档面) |
| 完整图像 / 音视频 / ML | 体积与依赖爆炸 |
| 任意第三方云 SDK | 不属于语言 |
### 3.3 插件仍保留的边界
ABI 插件 **不废除**。规则收紧为:
```text
若能力可用「少量 host_* + lib/*.mq.md」表达 → 必须走中层,禁止新开官方插件。
若能力是领域数值核 / 长生命周期服务 / 可选重依赖 → 才允许官方插件。
```
`lib/plugin` 继续服务:本地实验、第三方原生、官方领域包。
---
## 4. 实现分工(怎么长)
```text
用户 / catalog / 官方 ext(纯 mq 优先)
│ import re:lib/re.mq.md
▼
中层 L1(.mq.md,中英分文件,除 json 类共用外)
│ **> host_re_find …**
▼
L0.5 宿主(Rust:regex / sha2 / …)
│
▼
OS / 算法库(精选 crate,默认 features 最小化)
```
| 规则 | 说明 |
|------|------|
| `ext/**` 仍禁止 `host_*` | 中层补齐后,ext 经 `lib/re` 等调用 |
| 每个新 `host_*` | 必须有 L1 包装 + 金样 + 中英对称(见 [stdlib-i18n.md](stdlib-i18n.md)) |
| 能纯 mq 的 | 例如在 `table` 上的 `sort_by` 若性能可接受,可先纯库;热路径再下沉 host |
| WASM | 新 host 须标注是否进浏览器构建;不能 WASM 的在文档标 `host-only` |
| 依赖 | 新增 crate 写进 [dependencies.md](dependencies.md) 动机;禁止「顺手」拉大型框架 |
---
## 5. 模块候选总表(按优先级波次)
完整排期与验收见 [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md)。摘要:
| 波次 | 模块(英 / 中) | 典型能力 | 主要形态 |
|------|-----------------|----------|----------|
| **M1** | `re` / `正则` · `encoding` / `编码` | match / find / replace;base64 / hex | **done** |
| **M2** | `path` / `路径` · `fs` 加厚 | join / normalize;copy / move / temp | **done** |
| **M3** | `hash` / `哈希` · `secrets` / `机密` | sha256 / hmac;token | **done** |
| **M4** | `csv` / `逗号表` · `text` 加厚 | parse / stringify;contains / replace | **done** |
| **M5** | `table` 加厚 · `cli` / `命令行` | sort / zip;flags | **done** |
| **M6** | `log` / `日志` · `uuid` / `标识` | level 日志;v4 | 薄 host 或纯 mq |
| **可选** | `yaml` / `Toml` | 仅当 public/catalog 强需求 | 慎入 |
**刻意延后:** 完整 `datetime` 时区库、连接池、异步 runtime、模板引擎、完整 `collections` 类型系统——等中层前几波用起来再议。
---
## 6. 与现有库的关系
| 现有 | 中层策略 |
|------|----------|
| `lib/text` | **加厚**,不另起 `strings` 英文名(保持 Marqdo 习惯);中文 `文本` |
| `lib/table` | Mid 集合算法优先挂这里或薄 `listx`;避免与 GFM 表语义打架 |
| `lib/net` | HTTP 客户端保留;编解码类能力与 `encoding` 边界写清(url 组件归谁) |
| `lib/math` | 保持**高中**;哈希 / 安全随机不进 math |
| `lib/json` | 仍只 parse/stringify/quote;CSV/YAML 另库 |
| `lib/plugin` | 不变;文档写明「通用能力请先查 Mid」 |
| `ext/*` | 逐步**改写依赖**:能改用 `lib/re` 的路由/校验,去掉不必要的原生 |
---
## 7. 文档与工程节奏
每个 Mid 波次固定流水线(与现有发版习惯对齐):
1. 本设计不变的前提下,在 roadmap 勾选该波次工作项。
2. `doc/design/stdlib-<module>.md`(可短)→ `host_*` → `lib/` 中英 → `tests/lib/*` 金样 → `public/features` 短文。
3. 更新 [stdlib-modules.md](stdlib-modules.md) 模块表。
4. **不**为 Mid 单开 SemVer 故事;随正常产品发版收进 CHANGELOG「标准库」小节。
5. 发版技能 / Release 资产:仅当新增 **必须** 随 CLI 分发的东西时改 release 脚本——Mid 默认已嵌入 stdlib,**无** native zip。
---
## 8. 成功标准(数个版本后)
1. 用 Mid + 核心 L1 **不加载任何插件**,能写:日志采集脚本、CSV 清洗、带正则的文本管线、带哈希校验的下载器、简单 CLI 工具。
2. 新的官方 **非领域** 能力默认进 `lib/`,评审时问:「能否不做成插件?」
3. `ext/web` / `ext/agent` 等仍可有插件,但依赖面写进文档:哪些能力已改走 Mid。
4. 主二进制体积与依赖可解释;WASM 可用性有清单。
---
## 9. 一句话
**中层标准库把「通用原语」沉回语言自带层,让插件专属于真正的领域引擎——这样 Marqdo 才像一门通用语言,而不是「Markdown + 一堆 .so」。**
+19 -8
View File
@@ -2,10 +2,10 @@
| | | | | |
|---|---| |---|---|
| 状态 | **已落地**:text/table/fs/sys/time/json/net/math/foreign/**plugin**/browser | | 状态 | **核心已落地**;**中层 Mid:M1–M6 已落地** · [stdlib-mid.md](stdlib-mid.md) / [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
| 日期 | 2026-08-07 | | 日期 | 2026-09-07 |
| 原则 | 全部经 frontmatter **导入**;除 JSON 外中英分文件;内核保持少而精 | | 原则 | 全部经 frontmatter **导入**;除 JSON 外中英分文件;内核保持少而精;**通用能力优先 Mid,插件留给领域核** |
| 相关 | [stdlib.md](stdlib.md) · [stdlib-i18n.md](stdlib-i18n.md) · [ext-abi.md](ext-abi.md) · [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [**module-namespace.md**](module-namespace.md)(导入命名空间;**M1/M2 已落地**:`库.成员` / `库.对象.成员` / `use`) | | 相关 | [stdlib.md](stdlib.md) · [stdlib-i18n.md](stdlib-i18n.md) · [stdlib-mid.md](stdlib-mid.md) · [ext-abi.md](ext-abi.md) · [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [**module-namespace.md**](module-namespace.md)(导入命名空间;**M1/M2 已落地**:`库.成员` / `库.对象.成员` / `use`) |
--- ---
@@ -15,10 +15,19 @@
| 模块族 | 英文库 | 中文库 | 状态 | | 模块族 | 英文库 | 中文库 | 状态 |
|--------|--------|--------|------| |--------|--------|--------|------|
| 文本 | `lib/text.mq.md` | `lib/文本.mq.md` | 已有 | | 文本 | `lib/text.mq.md` | `lib/文本.mq.md` | 已有 + **Mid M4** 加厚 |
| 表 | `lib/table.mq.md` | `lib/表.mq.md` | 已有(`put`/`改` + list/map;见 [stdlib-table.md](stdlib-table.md)) | | **正则** | `lib/re.mq.md` | `lib/正则.mq.md` | **Mid M1** |
| **编解码** | `lib/encoding.mq.md` | `lib/编码.mq.md` | **Mid M1** |
| **路径** | `lib/path.mq.md` | `lib/路径.mq.md` | **Mid M2** |
| **哈希** | `lib/hash.mq.md` | `lib/哈希.mq.md` | **Mid M3** |
| **机密** | `lib/secrets.mq.md` | `lib/机密.mq.md` | **Mid M3** |
| **逗号表** | `lib/csv.mq.md` | `lib/逗号表.mq.md` | **Mid M4** |
| 表 | `lib/table.mq.md` | `lib/表.mq.md` | 已有 + Mid M4/M5:`sort`/`unique`/`zip`/… |
| **命令行** | `lib/cli.mq.md` | `lib/命令行.mq.md` | **Mid M5** |
| **日志** | `lib/log.mq.md` | `lib/日志.mq.md` | **Mid M6** |
| **标识** | `lib/uuid.mq.md` | `lib/标识.mq.md` | **Mid M6** |
| 浏览器效应 | `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM 客户端效应助手(GFM + helpers;勿用 `json.set` 链) | | 浏览器效应 | `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM 客户端效应助手(GFM + helpers;勿用 `json.set` 链) |
| 文件 | `lib/fs.mq.md` | `lib/文件.mq.md` | 已有 | | 文件 | `lib/fs.mq.md` | `lib/文件.mq.md` | 已有 + Mid M2:`copy_file` / `move` / `make_temp` |
| 系统 | `lib/sys.mq.md` | `lib/系统.mq.md` | 已有(含 `load_dotenv`) | | 系统 | `lib/sys.mq.md` | `lib/系统.mq.md` | 已有(含 `load_dotenv`) |
| 时间 | `lib/time.mq.md` | `lib/时间.mq.md` | 已有 | | 时间 | `lib/time.mq.md` | `lib/时间.mq.md` | 已有 |
| JSON | `lib/json.mq.md` | **同文件** | **仅** parse / stringify / quote;集合请用 table / GFM | | JSON | `lib/json.mq.md` | **同文件** | **仅** parse / stringify / quote;集合请用 table / GFM |
@@ -29,7 +38,9 @@
| **自写回** | `lib/writeback.mq.md` | `lib/自写回.mq.md` | **已有**(Jupyter 式输出写回) | | **自写回** | `lib/writeback.mq.md` | `lib/自写回.mq.md` | **已有**(Jupyter 式输出写回) |
| **子任务** | `lib/subtask.mq.md` | `lib/子任务.mq.md` | **已有**(OS 子进程 + KillOnDrop) | | **子任务** | `lib/subtask.mq.md` | `lib/子任务.mq.md` | **已有**(OS 子进程 + KillOnDrop) |
官方可选扩展(**非** stdlib):`ext/llm` · `ext/agent` — 见 [ext-llm.md](ext-llm.md) / [ext-agent.md](ext-agent.md)。 官方可选扩展(**非** stdlib):`ext/llm` · `ext/agent` · `ext/web` · `ext/quantum` · `ext/linalg` — 见各 `ext-*.md`。
**中层已落地:** M1–M6(含 log / uuid)— 见 [stdlib-mid.md](stdlib-mid.md) · [stdlib-log-uuid.md](stdlib-log-uuid.md)。
设计细节:[stdlib-writeback.md](stdlib-writeback.md) · [stdlib-subtask.md](stdlib-subtask.md)。 设计细节:[stdlib-writeback.md](stdlib-writeback.md) · [stdlib-subtask.md](stdlib-subtask.md)。
+33
View File
@@ -0,0 +1,33 @@
# `lib/path` — 路径代数(Mid M2)
| | |
|---|---|
| 状态 | **已落地(M2)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
| 导入 | `lib/path.mq.md` · `lib/路径.mq.md` |
| Host | `host_path_*`(`std::path`;无 I/O、不经 `fs_root`) |
---
## 1. API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `join` | `拼接` | `a`, `b` | 文本路径 |
| `split` | `拆分` | `path` | 组件文本列表(去空段) |
| `file_name` | `文件名` | `path` | 最后一段;根路径可为空文本 |
| `parent` | `父目录` | `path` | 父路径文本;无父则 `"."` |
| `extension` | `扩展名` | `path` | 不含点;无扩展名为 `""` |
| `normalize` | `规范化` | `path` | 清理 `.` / `..`(纯字符串,不访问磁盘) |
| `is_absolute` | `是绝对` | `path` | 布尔 |
## 2. 语义
- 分隔符与绝对路径判定跟随 **宿主 OS**(Linux CI 为 `/`)。
- **不做** I/O;逃逸检查仍由 `lib/fs` 在读写时执行。
- `join`:若 `b` 为绝对路径,结果为 `b`(同 `Path::join`)。
## 3. 刻意不做(v1)
`glob`、符号链接解析、`relative_to`、Windows UNC 专项文档。
+33
View File
@@ -0,0 +1,33 @@
# `lib/re` — 正则(Mid M1)
| | |
|---|---|
| 状态 | **已落地(M1)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
| 导入 | `lib/re.mq.md` · `lib/正则.mq.md` |
| Host | `host_re_*`(`regex` crate,默认 Unicode) |
---
## 1. API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `is_match` | `匹配` | `text`, `pattern` | 布尔 |
| `find` | `查找` | `text`, `pattern` | 首个匹配文本,或 `None` |
| `find_all` | `查找全部` | `text`, `pattern` | 文本列表 |
| `replace` | `替换` | `text`, `pattern`, `with`, 可选 `count` | 文本(`count` 缺省 = 全部) |
| `split` | `拆分` | `text`, `pattern` | 文本列表 |
非法 pattern → 诊断失败(含 crate 错误信息)。
## 2. 语义
- 引擎:Rust [`regex`](https://docs.rs/regex)(无回溯引用;与 RE2 同族限制)。
- 按 **UTF-8 文本**匹配,不做字节模式。
- `find` / `find_all` 返回整段匹配(不含捕获组拆分;捕获组 v1 不做)。
## 3. 刻意不做(v1)
命名捕获、替换回调、`Regex` 对象缓存 API、字节模式。
+24
View File
@@ -0,0 +1,24 @@
# `lib/secrets` — 安全随机令牌(Mid M3)
| | |
|---|---|
| 状态 | **已落地(M3)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-hash.md](stdlib-hash.md) · [stdlib-mid.md](stdlib-mid.md) |
| 导入 | `lib/secrets.mq.md` · `lib/机密.mq.md` |
| Host | `host_secrets_*`(`getrandom`) |
---
## 1. API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `token_hex` | `十六进制令牌` | 可选 `n`(字节数,默认 16) | 小写 hex,长度 `2*n` |
| `token_urlsafe` | `网址安全令牌` | 可选 `n`(字节数,默认 16) | URL-safe Base64(无填充) |
**不要**用 `math.random` / `math.seed` 生成令牌。
## 2. 刻意不做
密钥派生、加密/解密、密码哈希。
+30
View File
@@ -0,0 +1,30 @@
# `lib/text` 加厚(Mid M4)
| | |
|---|---|
| 状态 | **已落地(M4)** |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-csv.md](stdlib-csv.md) · [stdlib-mid.md](stdlib-mid.md) |
| 导入 | `lib/text.mq.md` · `lib/文本.mq.md` |
| Host | `host_text_*`(Unicode 大小写 / 子串;非正则替换) |
---
## 1. 新增 API
| 英文 | 中文 | 形参 | 结果 |
|------|------|------|------|
| `contains` | `包含` | `text`, `sub` | 布尔 |
| `starts_with` | `开头是` | `text`, `prefix` | 布尔 |
| `ends_with` | `结尾是` | `text`, `suffix` | 布尔 |
| `replace` | `替换` | `text`, `old`, `new`, 可选 `count` | 文本(字面替换,非正则) |
| `to_upper` | `大写` | `text` | 文本(Unicode) |
| `to_lower` | `小写` | `text` | 文本 |
| `repeat` | `重复` | `text`, `n` | 文本 |
| `pad` | `填充` | `text`, `width`, 可选 `fill`=`" "`,可选 `align`=`left`/`right`/`center` | 文本 |
保留原有 `str_trim` / `str_split` / `str_join`(及中文去空白/拆分/拼接)。
## 2. 刻意不做
正则替换(用 `lib/re`)、完整 `format` 模板引擎。
+8 -6
View File
@@ -2,12 +2,13 @@
| | | | | |
|---|---| |---|---|
| 状态 | **S0–S3 + L1 全模块落地** | | 状态 | **S0–S3 + 核心 L1 已落地 · 中层 L1(Mid)规划中** |
| 日期 | 2026-08-07 | | 日期 | 2026-09-07 |
| 相关 | [keywords.md](keywords.md) · [objects.md](objects.md) · [call-arguments.md](call-arguments.md) · [stdlib-i18n.md](stdlib-i18n.md) · [stdlib-modules.md](stdlib-modules.md) · [module-namespace.md](module-namespace.md) · [ext-abi.md](ext-abi.md) | | 相关 | [keywords.md](keywords.md) · [objects.md](objects.md) · [call-arguments.md](call-arguments.md) · [stdlib-i18n.md](stdlib-i18n.md) · [stdlib-modules.md](stdlib-modules.md) · [**stdlib-mid.md**](stdlib-mid.md) · [module-namespace.md](module-namespace.md) · [ext-abi.md](ext-abi.md) |
> **多语言库命名**:见 [stdlib-i18n.md](stdlib-i18n.md)(不设 `lang:`;中英分文件)。 > **多语言库命名**:见 [stdlib-i18n.md](stdlib-i18n.md)(不设 `lang:`;中英分文件)。
> **L1 模块清单**(含 math/foreign/plugin):见 [stdlib-modules.md](stdlib-modules.md)。 > **L1 模块清单**(含 math/foreign/plugin):见 [stdlib-modules.md](stdlib-modules.md)。
> **中层标准库(数个版本)**:通用原语沉 `lib/`、减少「事事插件」——设计 [stdlib-mid.md](stdlib-mid.md) · 路线 [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md)。
> **`ext/` 不是 stdlib**:官方可选扩展见 [ext-llm.md](ext-llm.md) / [ext-agent.md](ext-agent.md)。 > **`ext/` 不是 stdlib**:官方可选扩展见 [ext-llm.md](ext-llm.md) / [ext-agent.md](ext-agent.md)。
--- ---
@@ -17,9 +18,10 @@
| 层 | 内容 | | 层 | 内容 |
|----|------| |----|------|
| **L0 内置** | 解释器内:`print` `input` `len` `str` `int` `type` `trim` `split` `join` `at`(`type` 可含 `map`) | | **L0 内置** | 解释器内:`print` `input` `len` `str` `int` `type` `trim` `split` `join` `at`(`type` 可含 `map`) |
| **L1 官方库** | 仓库根目录 [`lib/`](../../lib/)(`.mq.md`);可用 `lib/…` 或别名 `std/…` 导入 | | **L1 核心** | 已有 [`lib/`](../../lib/):fs/sys/time/json/net/math/foreign/plugin/table/text/… |
| **官方 ext** | [`ext/`](../../ext/)(`.mq.md` + 可选原生插件);`MARQDO_EXT`;**不属于** L1 | | **L1 中层(Mid)** | 规划中:re / encoding / path / hash / csv / … — 仍在 `lib/`,见 [stdlib-mid.md](stdlib-mid.md) |
| **L2** | catalog / 用户站收录(随发版) | | **官方 ext** | [`ext/`](../../ext/)(`.mq.md` + 可选原生插件);`MARQDO_EXT`;**不属于** L1;插件仅领域热路径 |
| **L2** | catalog / 用户站收录(随发版);应主要靠核心+中层、默认无插件 |
--- ---
+113
View File
@@ -0,0 +1,113 @@
# 调研:线性代数 × 公式类 × 展示
| | |
|---|---|
| 状态 | 调研纪要(为 [ext-linalg](../design/ext-linalg.md) 供输入) |
| 日期 | 2026-09-07 |
| 相关 | [stdlib-math.md](../design/stdlib-math.md) · [ext-quantum-q7.md](../design/ext-quantum-q7.md) · SymPy MatrixExpr · Symbolics.jl · LAFigureSpecs |
---
## 1. 问题陈述
Marqdo 已有:
- `Value::Formula` + `` `f` = `` + `$$…$$`(高中标量符号)
- `formula::Expr::Matrix`:**数值**稠密矩阵字面量(`[[…]]` / `\begin{pmatrix}`),供量子门核对与 view KaTeX
- `ext/quantum` Q7:**量子语境**下的密度矩阵 / Kronecker / 谱 / SVD 风格图(ABI)
缺口:一门**经典线性代数**扩展——作者用**公式级矩阵表达式**写 \(A^\top A\)、\((X^\top X)^{-1}X^\top y\)、分块、Kronecker,在**不立刻展开成巨大稠密矩阵**的前提下做重写与化简,并在文档 / view 中**好看地呈现**分解与结构。
---
## 2. 业界对照
### 2.1 符号矩阵表达式(大规模优化的关键)
| 系统 | 模型 | 对 Marqdo 的启示 |
|------|------|------------------|
| **SymPy `MatrixExpr`** | `MatrixSymbol(name, n, m)` + `MatMul` / `MatAdd` / `Inverse` / `Transpose` / `Trace` / `Determinant` / `BlockMatrix` / `KroneckerProduct`;`as_explicit()` 才展开 | **默认保持抽象**;形状元数据传播;显式求值是可选动作 |
| SymPy `block_collapse` | 分块表达式在块级化简,避免整块稠密展开 | 分块是教学与优化的第一公民 |
| SymPy Kronecker | `KroneckerProduct` 惰性 + `combine_kronecker` 规则 | Kronecker **绝不能**默认 `expand`(维数爆炸) |
| SymPy 矩阵求导 | 经 array expr 再收回 MatrixExpr;闭式导数 | v1 **不做**;记入远期 |
| **Symbolics.jl** | 「数组的符号」vs「符号数组」:后者 O(1) 表示整阵,保留 `A*B` 不展开 | 与 MatrixExpr 同构;强调 **shape / eltype 元数据** |
| NumPy / Eigen / nalgebra | 数值热路径,无公式树 | 对应 Marqdo **插件数值层**,不是文档作者主路径 |
**结论:** 「公式类线性代数」= **抽象表达式 DAG + 形状检查 + 重写规则**;稠密数值是 `eval` / `as_explicit` 的终点,不是默认形态。
### 2.2 教学与展示(效果最好的做法)
| 做法 | 来源 | 效果 | Marqdo 映射 |
|------|------|------|-------------|
| KaTeX / MathJax 渲染 `bmatrix` / `pmatrix` | SymPy `latex()` + Jupyter `init_printing` | 公式即排版;多式用 `display` 并列 | 既有 view `formula-card` + KaTeX |
| 标签化等式 `A = UΣV^\top` | 教材 / Qiita display+Math | 步骤可读 | `show` / writeback 多卡片 |
| **结构图**(SVD / QR / 消元轨) | [LAFigureSpecs](https://github.com/ea42gh/LAFigureSpecs) + matrixlayout;TeX `drawmatrix` | 比数字表更能讲「秩 / 零空间 / 对角结构」 | SVG `kind=svd|qr|ge|eig` |
| 热力图 / Hinton | Qiskit / QuTiP / 既有 quantum | 稠密数值矩阵的幅度/符号 | `kind=heatmap|hinton`(经典实/复) |
| 分块着色矩形 | CTAN `drawmatrix` | 零块 / 三角 / 带状一目了然 | `kind=structure`(形状示意图,可无元素值) |
| 仅打印 ASCII | CLI 默认 | 金样可断言 | `str` / `print` → 规范 ASCII / LaTeX 源 |
**展示优先级(锁定建议):**
1. **符号式**:始终可 `latex` → view KaTeX(不依赖数值)
2. **结构式 SVG**:分解 / 分块 / 秩示意(教学主图)
3. **数值热力图**:仅在 `eval` 后、维数可控时
4. CLI:短 ASCII;大矩阵默认摘要 `m×n …` + 可选 `preview=`
### 2.3 一门「够用」的线性代数库需要什么
按大学一学期 + 数据科学入门,能力桶:
| 桶 | 内容 | 符号层 | 数值层 |
|----|------|--------|--------|
| A 构造 | 符号阵、常数阵、单位/零/对角、从 `$$` / 表 / list | ✅ | ✅ |
| B 代数 | 加减乘、转置、逆、伴随、幂、Kronecker、分块 | ✅ 重写 | ✅ |
| C 分解 | LU / QR / 特征 / SVD / Cholesky(对称) | 结果对象 + 结构图 | ✅ |
| D 系统 | `Ax=b`、最小二乘、伪逆 | 公式写法 + 数值解 | ✅ |
| E 度量 | 行列式、迹、秩、范数、条件数 | 部分可符号 | ✅ |
| F 展示 | latex / ASCII / SVG 结构 / heatmap | ✅ | ✅ |
| G 微积分 | 矩阵导数、Jacobian 积木 | 远期 | — |
**不做(首版):** 稀疏专用格式、GPU、分布式、完整 CAS 证明、通用张量网络、与 `lib/math` 高中标量混成一个巨模块。
---
## 3. 与现有 Marqdo 的衔接点
| 已有 | 用法 | 扩展时注意 |
|------|------|------------|
| `$$` + Matrix 数值字面量 | 小门矩阵、手算核对 | 保留;符号阵用 `MatrixSymbol` 或 `matrix.symbol`,勿与数值 Matrix 混为一谈 |
| `lib/math` formula | 标量 simplify/diff/plot | **不**把大学线性代数塞进 `lib/math`(与 Q7 结论一致) |
| view KaTeX | formula-card | 符号 MatExpr 的 `latex` 字符串直接喂 KaTeX |
| `ext/quantum` Q7 linop | 量子密度 / 纠缠 | **经典 API 不复用 quantum 命名空间**;共享算法可在插件内私有复用,作者面分离 |
| `plugins/*` ABI | 重数值 / SVG | 稠密分解与大图走 `plugins/linalg` |
| GFM 表 | 数据即代码 | 矩阵输入:表或 `$$`;分解步骤:`@` 记录表 |
---
## 4. 「大规模优化」具体指什么
在公式类语境下,优化 ≠ BLAS 微内核,而是:
1. **惰性**:`A*B*C` 保持乘积树,直到 `eval` / `explicit`
2. **形状短路**:不相容维数在符号阶段报错,不跑数值
3. **代数恒等**:`(A^{-1})^{-1}→A`,`(AB)^\top→B^\top A^\top`,`I*A→A`,分块 `block_collapse`
4. **禁止爆炸展开**:Kronecker / 高维符号积默认不 `expand`
5. **数值批处理**:同形状多 RHS、分解一次多用(`factorize` 对象)
6. **展示成本**:大矩阵不默认内嵌完整 KaTeX 数字阵;结构图 + 摘要
---
## 5. 推荐产品叙事(一句)
**线性代数文档用公式写变换,用规则做化简,用结构图讲分解,用可控数值做验算——公式是一等公民,稠密矩阵是可选终点。**
---
## 6. 参考链接
- SymPy Matrix Expressions: https://docs.sympy.org/latest/modules/matrices/expressions.html
- SymPy Matrix derivatives (远期): https://docs.sympy.org/dev/explanation/modules/matrices/matrixderivatives.html
- Symbolics.jl Symbolic Arrays: https://docs.sciml.ai/Symbolics/v6.54/manual/arrays/
- LAFigureSpecs(教学结构图): https://github.com/ea42gh/LAFigureSpecs
- CTAN drawmatrix: 矩阵形状示意
- 既有:`doc/design/stdlib-math.md` · `doc/design/ext-quantum-q7.md`
+126
View File
@@ -0,0 +1,126 @@
# 官方扩展:`ext/linalg` 实现路线图
| | |
|---|---|
| 状态 | **L0–L6 + 公式文档面 F1–F3 已落地 · L7+ 远期** |
| 日期 | 2026-09-07 |
| **锁定设计** | [design/ext-linalg.md](../design/ext-linalg.md) · [design/ext-linalg-formula-doc.md](../design/ext-linalg-formula-doc.md) |
| 调研 | [research/ext-linalg-formula.md](../research/ext-linalg-formula.md) |
| 相关 | [ext-cli.md](../design/ext-cli.md) · [ext-abi.md](../design/ext-abi.md) · [stdlib-math.md](../design/stdlib-math.md) · [view.md](../design/view.md) · [ext-quantum-q7.md](../design/ext-quantum-q7.md) |
| 安装(规划) | `marqdo ext add linalg`(`linalg` / `线性代数`) |
本文只跟踪**实现阶段**。作者面、公式规则、展示以设计文为准。
---
## 1. 目标回顾(一句)
**可运行的线性代数公式文档**:矩阵公式可化简、可 KaTeX 展示;跑通即在规则集内正确;需要时再稠密求值与分解,并内嵌结构 SVG。
---
## 2. 落地阶段
| 阶段 | 内容 | 状态 |
|------|------|------|
| **L0** | CATALOG + `plugins/linalg` ping + L1 `ensure_plugin`;`ext add` 条目 | **done** |
| **L1** | MatExpr:`symbol` / 稠密叶子 / `mul`·`add`·`T`·`inv`;`simplify` R1–R5;`latex`·`ascii`·`show` | **done** |
| **L2** | `explicit` / 小阵 gemm·解系·`det`·`trace`;维数护栏;金样 `linalg-basic-smoke` | **done** |
| **L3** | `block` + `collapse`;`kron` 惰性 + R7;金样 `linalg-block-kron` | **done** |
| **L4** | `factorize` LU/QR/SVD/eig(+ 可选 chol);结构 SVG `svd|qr|ge|eig`;金样 `linalg-factor-smoke` | **done** |
| **L5** | `heatmap`/`hinton`;examples(最小二乘 / SVD 示意);public + skill 摘要 | **done** |
| **L6** | 复数 dtype;`lstsq`;`norm`/`cond`;主题令牌对齐 | **done** |
| **F1** | 展示路径默认 simplify;公式文档金样 | **done** |
| **F2** | 符号引入降噪;examples 讲义化 | **done** |
| **F3** | 紧凑糖 + public「如何写公式文档」 | **done** |
| **L7+** | 矩阵微积分 / 更大 BLAS 依赖(仅当用户明确要求) | **deferred** |
验收金样(规划路径):`tests/ext/linalg-*-smoke.mq.md`;示例:`examples/linalg-least-squares/`、`examples/linalg-svd/`。
---
## 3. 每阶段出口检查
### L0
- [x] `ext/linalg` CATALOG 含 `linalg`
- [x] 插件 `ping` → ok
- [x] 文档:`marqdo ext add linalg`
### L1
- [x] `(AB)^\top` → `simplify` → `B^T*A^T`(无 dense)
- [x] 形状错误含行列信息
- [x] CLI `ascii` / `show` SVG 通道
- [x] 中英 L1 各一 smoke
### L2
- [x] `from_formula` 吸收 `$$` 数值阵
- [x] `solve` 与手算 2×2 一致
- [x] 超维 `explicit` 硬错误(单元测护栏)
### L3
- [x] 分块乘 `collapse` 后块级正确
- [x] Kronecker 默认不膨胀元素个数(惰性 `A⊗B`)
### L4
- [x] SVD/特征结果对象字段稳定
- [x] SVG 含可断言标记(如 `data-linalg="eig"`)
- [x] `factorize` + 二次 `solve` 复用(LU)
### L5
- [x] 至少 1 个 examples 可 `marqdo run`
- [x] public/features 或 tutorial 短页
- [x] skill / CHANGELOG Unreleased 条目
### L6
- [x] `lstsq` / `norm` / `cond` / `rank` 金样
- [x] `dtype=complex` 基线(Frobenius;分解仍为实数)
- [x] `draw theme=light|dark|bw` 与 quantum Q8 令牌对齐(`data-theme`)
### F1
- [x] `ascii` / `latex` / `show` 默认 simplify
- [x] 金样:`.T` 后直接 `ascii` 得 `B^T*A^T`(无需显式 simplify)
- [x] 设计文 [ext-linalg-formula-doc.md](../design/ext-linalg-formula-doc.md) 落地
### F2
- [x] 符号引入降噪(选定候选) — GFM `@` 表 + `la.declare`
- [x] 至少 1 个 example 以代数面为主叙述 — `examples/linalg-transpose/`
### F3
- [x] `T_ascii` / `转置文本` 一步展示糖
- [x] `explicit` 默认先 simplify(`raw=` 可关)
- [x] public `07-linalg` / `07-线性代数`「如何写公式文档」
---
## 4. 依赖与并行
| 依赖 | 说明 |
|------|------|
| ABI v2 + ext CLI | 已有 |
| view KaTeX / plot | 已有;L1 `show` 接现成通道 |
| Release native zip | 随首次含 `plugins/linalg` 的发版接入 |
| quantum 数值核 | **可选**私有复用;不阻塞 L1–L2 |
不阻塞项:浏览器 WASM 上的 linalg(默认 host/native;WASM 另议)。
---
## 5. 建议开发顺序(下一手)
1. **L7+** 仅当用户明确要求更深符号(微积分等)。公式文档面 F1–F3 已收口。
---
## 6. 一句话
**代数面像讲义、计算面是步骤;跑通即在规则集内正确;每阶段都有可跑金样。**
+1 -1
View File
@@ -18,7 +18,7 @@
| **诊断** | 语句级 `path:line:col: message`;字节码指令带 `spans`;errors 金样例双后端断言位置 | **P0 完成** | | **诊断** | 语句级 `path:line:col: message`;字节码指令带 `spans`;errors 金样例双后端断言位置 | **P0 完成** |
| **view** | 结构 + 执行 + 源码 + 函数大纲/搜索;无断点 | 文档浏览器 | | **view** | 结构 + 执行 + 源码 + 函数大纲/搜索;无断点 | 文档浏览器 |
| **debug** | 独立 `marqdo debug`:断点 / 单步 / locals(树遍历) | **P3-D3 v1** | | **debug** | 独立 `marqdo debug`:断点 / 单步 / locals(树遍历) | **P3-D3 v1** |
| **标准库** | S0–S3 完成;本波扩展 = 文件/系统/时间/JSON/网络([stdlib-modules.md](../design/stdlib-modules.md));数学/外联暂缓 | **P2 完成;五库扩展设计中** | | **标准库** | 核心 L1 已完成([stdlib-modules.md](../design/stdlib-modules.md));下一主线 = **中层 Mid**([stdlib-mid.md](../design/stdlib-mid.md) · [stdlib-mid 路线](stdlib-mid.md)) | **P2 完成;Mid M1–M6 规划中** |
| **模块** | [module-namespace.md](../design/module-namespace.md):**M1+M2 已落地**(库名绑定、`库.成员` / `use`;无扁平合并) | — | | **模块** | [module-namespace.md](../design/module-namespace.md):**M1+M2 已落地**(库名绑定、`库.成员` / `use`;无扁平合并) | — |
用户点名的三件事都对,但**不能并行硬开**:断点调试依赖「可信位置 + 可暂停执行」;标准库依赖稳定运行时与 I/O;`input` 在 CLI 与 view 是两条路径。 用户点名的三件事都对,但**不能并行硬开**:断点调试依赖「可信位置 + 可暂停执行」;标准库依赖稳定运行时与 I/O;`input` 在 CLI 与 view 是两条路径。
+199
View File
@@ -0,0 +1,199 @@
# 路线图:中层标准库(数个版本)
| | |
|---|---|
| 状态 | **M1–M6 已落地** |
| 日期 | 2026-09-07 |
| 设计 | [stdlib-mid.md](../design/stdlib-mid.md) |
| 相关 | [stdlib-modules.md](../design/stdlib-modules.md) · [next-phase.md](next-phase.md) · [ext-cli.md](../design/ext-cli.md) |
> **不预标 SemVer。** 每一波随当时产品发版合入;下文用 **M1…M6** 波次名。每波须可单独演示、可回滚。
---
## 0. 总序
```text
M1 文本与编解码地基 re + encoding ← 最先解锁「无插件写库」
M2 路径与文件中层 path + fs 加厚
M3 完整性与机密 hash + secrets
M4 表格数据与文本加厚 csv + text++
M5 集合与 CLI 糖 table/listx + cli
M6 可观测与标识 log + uuid(+ 可选 yaml)
```
原则:
1. **先原语、后糖**:正则 / 编解码 / 路径先于 CLI 糖与日志格式。
2. **每波必须有金样 + public 短页**;无文档不算完成。
3. **并行约束**:不阻塞 `ext/linalg` 等已定领域工作;但新官方能力评审优先问 Mid。
4. **插件冻结规则**:自 M1 起,官方新增「通用」ABI 插件默认 **拒绝**,除非设计文档论证无法用 host+L1。
---
## 1. 现状缺口(开工依据)
| 能力 | 现状 | 后果 |
|------|------|------|
| 正则 | 无 | 校验、替换、简单解析只能 foreign 或插件 |
| base64 / hex | 无(仅有 `url_encode`) | 令牌、摘要、嵌二进制文本困难 |
| 路径代数 | `fs` 只做 I/O | 拼接靠字符串,跨平台易错 |
| 哈希 / HMAC | 无 | 校验、缓存键、签名只能外联 |
| CSV | 无 | 数据脚本推向外联 Python |
| `lib/text` | 3 个包装函数 | 「有 text 库」名不副实 |
| 安全随机 | 仅 `math.random` | 易误用于 token |
| 结构化日志 / flags | 无 | 每个脚本自造 |
对照:Go 日常高频包(`regexp`、`encoding/*`、`crypto`、`path/filepath`、`slices`)与 Python「难写对才进 stdlib」——正是 M1–M5 的选型来源;**不**把 HTTP 服务端、数据库驱动塞进 Mid(见设计文 §3.2)。
---
## 2. 波次明细
### M1 — 正则与编解码(最高优先) — **done**
| 交付 | 说明 |
|------|------|
| `lib/re.mq.md` / `lib/正则.mq.md` | `is_match` / `find` / `find_all` / `replace` / `split` |
| `lib/encoding.mq.md` / `lib/编码.mq.md` | `base64_*` / `hex_*` |
| Host | `host_re_*`(`regex`)、`host_encoding_*`(无第三方编解码 crate) |
| 金样 | `tests/lib/re-smoke` · `正则-烟测` · `encoding-smoke` · `编码-烟测` |
| 文档 | [stdlib-re.md](../design/stdlib-re.md) · [stdlib-encoding.md](../design/stdlib-encoding.md) · public `08-re-encoding` |
| 验收 | 无插件:邮箱式校验 + base64/hex 往返 |
**刻意不做:** 回溯引用极致兼容、替换回调、完整 PCRE 方言文档化——先 RE2/ Rust `regex` 语义并写清。
---
### M2 — 路径与文件中层 — **done**
| 交付 | 说明 |
|------|------|
| `lib/path.mq.md` / `lib/路径.mq.md` | `join` / `split` / `file_name` / `parent` / `extension` / `normalize` / `is_absolute` |
| `lib/fs` 加厚 | `copy_file` / `move` / `make_temp`(文本边界;无 `read_bytes`) |
| Host | `host_path_*`、`host_copy_file` / `host_move` / `host_make_temp` |
| 金样 | `path-smoke` · `路径-烟测` · `fs-copy-move` · `文件-复制` |
| 文档 | [stdlib-path.md](../design/stdlib-path.md) · [stdlib-fs-mid.md](../design/stdlib-fs-mid.md) · public `09-path-fs` |
| 验收 | 规范化 + 沙箱内复制/移动 |
**刻意不做:** 完整 `shutil`、监视 `inotify`、权限 ACL 全家桶。
---
### M3 — 哈希与机密 — **done**
| 交付 | 说明 |
|------|------|
| `lib/hash.mq.md` / `lib/哈希.mq.md` | `sha256` / `sha1` / `md5` / `hmac_sha256` |
| `lib/secrets.mq.md` / `lib/机密.mq.md` | `token_hex` / `token_urlsafe` |
| Host | `host_hash_*`、`host_secrets_*`(`sha2`/`sha1`/`md-5`/`hmac`/`getrandom`) |
| 金样 | `hash-smoke` · `哈希-烟测` · `secrets-smoke` · `机密-烟测` |
| 文档 | [stdlib-hash.md](../design/stdlib-hash.md) · [stdlib-secrets.md](../design/stdlib-secrets.md) · public `10-hash-secrets` |
| 验收 | 已知向量 + 令牌长度 |
**刻意不做:** 密码哈希(Argon2/bcrypt)、完整 TLS、证书管理——仍属服务端/安全产品。
---
### M4 — CSV 与文本加厚 — **done**
| 交付 | 说明 |
|------|------|
| `lib/csv.mq.md` / `lib/逗号表.mq.md` | `parse` / `stringify`(首行表头 → list of maps) |
| `lib/text` / `文本` 加厚 | `contains` / `starts_with` / `ends_with` / `replace` / `to_upper`/`to_lower` / `repeat` / `pad` |
| Host | `host_csv_*`、`host_text_*` |
| 金样 | `csv-smoke` · `逗号表-烟测` · `text-mid-smoke` · `文本-加厚-烟测` |
| 文档 | [stdlib-csv.md](../design/stdlib-csv.md) · [stdlib-text-mid.md](../design/stdlib-text-mid.md) · public `11-csv-text` |
| 验收 | CSV 往返 + 文本清洗小例 |
**可选同波:** 极简 `format`(`{name}` 替换),若与引用语法冲突则延后。
---
### M5 — 集合算法与 CLI — **done**
| 交付 | 说明 |
|------|------|
| `lib/table` / `表` 加厚 | `sort` / `sort_by` / `unique` / `chunk` / `zip` / `flatten`(`reverse` 已有) |
| `lib/cli.mq.md` / `lib/命令行.mq.md` | `parse`(`--k v` / `--k=v` / `--flag`;位置参 `_`) |
| Host | `host_list_sort*` / `unique` / `chunk` / `zip` / `flatten`、`host_cli_parse` |
| 金样 | `table-list-mid` · `cli-sort-smoke` · `表-命令行-烟测` |
| 文档 | [stdlib-list-cli.md](../design/stdlib-list-cli.md) · public `12-list-cli` |
| 验收 | CSV 按键排序 + 参数解析 |
**刻意不做:** 泛型迭代器协议、惰性流、完整 argparse 互斥组。
---
### M6 — 日志、UUID、收口 — **done**
| 交付 | 说明 |
|------|------|
| `lib/log.mq.md` / `lib/日志.mq.md` | `debug`/`info`/`warn`/`error`;level 过滤;一行文本 |
| `lib/uuid.mq.md` / `lib/标识.mq.md` | `v4` |
| Host | `host_log_set_level` / `host_log_line`、`host_uuid_v4` |
| 金样 | `log-uuid-smoke` · `日志-标识-烟测` |
| 文档 | [stdlib-log-uuid.md](../design/stdlib-log-uuid.md) · public `13-log-uuid` |
| 文档收口 | `stdlib-modules.md` 总表;设计文状态 → **M1–M6 已落地** |
| 可选未做 | `yaml` 只读子集(默认不做) |
---
## 3. 跨波次工程清单
每波共用:
- [ ] 中英 L1 对称([stdlib-i18n.md](../design/stdlib-i18n.md))
- [ ] `host_*` 形参在 L1 **全量转发**([stdlib-modules.md](../design/stdlib-modules.md) §2.1)
- [ ] 双后端金样(tree + bytecode,若该 host 已接线)
- [ ] WASM:标注 `host-only` 或实现 stub
- [ ] CHANGELOG「标准库」条目
- [ ] skill `examples.md` 增加 Mid 惯用写法(GFM 优先)
宿主膨胀守卫:
- 新增 crate 须在 PR/设计中写清:**为何不能纯 mq**、**features 如何裁剪**、**许可证**。
- 禁止借 Mid 把 `ext/web` 的服务端栈链进主二进制。
---
## 4. 与官方扩展的互动(减插件)
| 扩展 | Mid 落地后可做的减负 |
|------|----------------------|
| `ext/web` | 路由/校验用 `re`;ETag/缓存键用 `hash`;静态路径用 `path` |
| `ext/agent` | 工具参数校验、密钥材料用 `secrets`/`hash`(领域核仍插件) |
| `ext/ai/llm` | 头/体编解码走 `encoding` |
| `ext/linalg` / `quantum` | **不**迁入 Mid;继续插件;仅共用 `path`/`hash` 等通用面 |
跟踪方式:某波完成后,开短 issue/清单「ext X 可删除的原生辅助」,不强迫同波大改。
---
## 5. 建议人力切分(示意)
| 波次 | 大致体量 | 可演示产物 |
|------|----------|------------|
| M1 | 中 | 「正则替换 + base64 往返」用户页 |
| M2 | 中 | 「安全拼接路径写文件」 |
| M3 | 小–中 | 「下载内容 sha256 校验」 |
| M4 | 中 | 「CSV → 表 → 打印」 |
| M5 | 小–中 | 「零插件数据 CLI」 |
| M6 | 小 | 「带 log level 的脚本」 |
若只能开一条线:**M1 → M3 → M2 → M4 → M5 → M6**(完整性校验常比路径代数更刚需时,M3 可紧贴 M1)。
---
## 6. 完成定义(整条 Mid 叙事)
当 M1–M6 完成时,对外可宣称:
> Marqdo 自带中层标准库:正则、编解码、路径、哈希、CSV、加厚文本与集合/CLI 糖,以及日志与 UUID;**写通用脚本与多数扩展库不再需要原生插件。**
> 插件保留给 web 运行时、智能体、量子与线性代数等**领域引擎**。
---
## 7. 一句话
**按 M1→M6 把通用原语沉进 `lib/`,用数个发版周期换掉「事事插件」——领域 `ext/` 继续尖,语言本体变宽。**
+12
View File
@@ -0,0 +1,12 @@
# linalg-least-squares
Overdetermined least squares via `ext/linalg`: `factorize kind=qr`, `Qᵀb`, solve `R`, plus a heatmap SVG of `A`.
```bash
cargo build --release -p marqdo_plugin_linalg
marqdo ext add linalg # once (re-run after rebuilding the plugin)
marqdo run examples/linalg-least-squares/index.mq.md
# optional: marqdo view examples/linalg-least-squares/
```
Design: [ext-linalg.md](../../doc/design/ext-linalg.md) · sibling: [linalg-svd](../linalg-svd/).
+35
View File
@@ -0,0 +1,35 @@
---
title: Least squares via lstsq
description: Overdetermined Ax≈b with ext/linalg lstsq + heatmap.
import la:ext/linalg/linalg.mq.md
import math:lib/math.mq.md
---
# Least squares from a runnable document
Fit a line through three points with an overdetermined `A x ≈ b` using `lstsq` (QR path). A heatmap makes the design matrix visible in `view`.
# main
`A` =
$$
\begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix}
$$
`b` =
$$
\begin{pmatrix}1\\2\\2\end{pmatrix}
$$
*`A` = > la.from_formula formula=`A`*
*`b` = > la.from_formula formula=`b`*
*`_` = > la.draw factor=`A` kind="heatmap" theme="light" path="ls-heatmap.svg"*
*`ls` = > la.lstsq a=`A` b=`b`*
*`x` = ls[^x]*
*`resid` = ls[^residual_fro]*
> print text=`x`
> print text=`resid`
> print text=ls-ok
+12
View File
@@ -0,0 +1,12 @@
# linalg-svd
Dense SVD via `ext/linalg`: singular values + structure / hinton SVG.
```bash
cargo build --release -p marqdo_plugin_linalg
marqdo ext add linalg
marqdo run examples/linalg-svd/index.mq.md
# optional: marqdo view examples/linalg-svd/
```
Design: [ext-linalg.md](../../doc/design/ext-linalg.md) · sibling: [linalg-least-squares](../linalg-least-squares/).
+25
View File
@@ -0,0 +1,25 @@
---
title: SVD structure demo
description: Diagonal SVD + structure SVG via ext/linalg.
import la:ext/linalg/linalg.mq.md
---
# SVD from a runnable document
A diagonal matrix has singular values on the diagonal. Prefer matrix values and methods; `factorize` / `draw` stay explicit for teaching SVGs.
# main
`M` =
$$
\begin{bmatrix}4&0\\0&2\end{bmatrix}
$$
*`M` = > la.from_formula formula=`M`*
*`f` = > la.factorize matrix=`M` kind="svd"*
*`S` = f[^S]*
*`_` = > la.draw factor=`f` kind="svd" theme="light" path="svd-structure.svg"*
*`hm` = > la.draw factor=`M` kind="hinton" path="svd-hinton.svg"*
> print text=`S`
> print text=svd-ok
+11
View File
@@ -0,0 +1,11 @@
# linalg-transpose
Formula-document example: declare symbols with a GFM table, check \((AB)^\top = B^\top A^\top\) via `ascii` (auto-simplify).
```bash
cargo build --release -p marqdo_plugin_linalg
marqdo ext add linalg
marqdo run examples/linalg-transpose/index.mq.md
```
Design: [ext-linalg-formula-doc.md](../../doc/design/ext-linalg-formula-doc.md).
+29
View File
@@ -0,0 +1,29 @@
---
title: Transpose identity (formula document)
description: (AB)^T = B^T A^T as a runnable lecture fragment.
import la:ext/linalg/linalg.mq.md
---
# Transpose reverses products
Declare shapes in a table, form \(P = AB\), then check \((AB)^\top = B^\top A^\top\) by running the document.
# main
`shapes` =
| @ | name | rows | cols |
|---|------|------|------|
| 1 | A | 2 | 3 |
| 2 | B | 3 | 2 |
*`env` = > la.declare table=`shapes`*
*`A` = env[^A]*
*`B` = env[^B]*
*`P` = `A` * `B`*
*`t` = > `P`.T_ascii*
> print text=`t`
1. `t` == "B^T*A^T"
> print text=identity-ok
2. *
> print text=identity-fail
+12 -12
View File
@@ -6,7 +6,7 @@ import json:lib/json.mq.md
import sys:lib/sys.mq.md import sys:lib/sys.mq.md
import fs:lib/fs.mq.md import fs:lib/fs.mq.md
import time:lib/time.mq.md import time:lib/time.mq.md
import writeback:lib/writeback.mq.md import wb:lib/writeback.mq.md
import subtask:lib/subtask.mq.md import subtask:lib/subtask.mq.md
import plugin:lib/plugin.mq.md import plugin:lib/plugin.mq.md
--- ---
@@ -130,7 +130,7 @@ Allowlist check, then invoke the runbook `##` via `lib/subtask` (`spawn fn=` →
Build a runnable workbook. `skeleton=single` (default): one worker `step`. `skeleton=dual`: research then write agents. On success the parent chooses DONE; runtime `agent_workbook_solidify` freezes the answer. Build a runnable workbook. `skeleton=single` (default): one worker `step`. `skeleton=dual`: research then write agents. On success the parent chooses DONE; runtime `agent_workbook_solidify` freezes the answer.
*q = > json.quote text=`goal`* *q = > json.quote text=`goal`*
*esc = > json.parse text={"h":"---\ntitle: agent workbook\nimport llm:ext/ai/llm.mq.md\nimport agent:ext/ai/agent.mq.md\nimport json:lib/json.mq.md\nimport sys:lib/sys.mq.md\nimport writeback:lib/writeback.mq.md\nimport subtask:lib/subtask.mq.md\n---\n\n# Goal\n\nParent goal is embedded as JSON in # main.\n\n## Solidify\n\nWhen the child returns a good value, parent DECISION: DONE — runtime agent_workbook_solidify freezes the answer as **return** (do not invent long FIND/REPLACE).\n\n# main\n\n> llm.load_env\n\n","s1":"\u002a\u0060model\u0060 = > llm.llm \u002a\n","s2":"\u002a\u0060tools\u0060 = > json.parse text=[] \u002a\n","s3":"\u002a\u0060worker\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You are a workbook worker. tools=[] means never CALL. Finish the task with a direct final answer; do not invent tools. \u002a\n","s4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","s5":"} \u002a\n","s6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","s7":"\u002a\u0060out\u0060 = > \u0060worker\u0060.step task=\u0060task\u0060 stream=True \u002a\n","s7b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","s8":"\u002a\u002a\u0060text\u0060\u002a\u002a\n","d3":"\u002a\u0060research\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You gather facts for the goal. Be concise. Do not invent tools. \u002a\n","d3b":"\u002a\u0060writer\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You write the final answer from research notes. Do not invent tools. \u002a\n","d4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","d5":"} \u002a\n","d6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","d7":"\u002a\u0060notes\u0060 = > \u0060research\u0060.step task=\u0060task\u0060 stream=True \u002a\n","d8":"\u002a\u0060ns\u0060 = > json.stringify value=\u0060notes\u0060 \u002a\n","d9":"\u002a\u0060wt\u0060 = Write the final answer. Research notes: \u002a\n","d10":"\u002a\u0060wt\u0060 = \u0060wt\u0060 + \u0060ns\u0060 \u002a\n","d11":"\u002a\u0060out\u0060 = > \u0060writer\u0060.step task=\u0060wt\u0060 stream=True \u002a\n","d11b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","d12":"\u002a\u002a\u0060text\u0060\u002a\u002a\n"}* *esc = > json.parse text={"h":"---\ntitle: agent workbook\nimport llm:ext/ai/llm.mq.md\nimport agent:ext/ai/agent.mq.md\nimport json:lib/json.mq.md\nimport sys:lib/sys.mq.md\nimport wb:lib/writeback.mq.md\nimport subtask:lib/subtask.mq.md\n---\n\n# Goal\n\nParent goal is embedded as JSON in # main.\n\n## Solidify\n\nWhen the child returns a good value, parent DECISION: DONE — runtime agent_workbook_solidify freezes the answer as **return** (do not invent long FIND/REPLACE).\n\n# main\n\n> llm.load_env\n\n","s1":"\u002a\u0060model\u0060 = > llm.llm \u002a\n","s2":"\u002a\u0060tools\u0060 = > json.parse text=[] \u002a\n","s3":"\u002a\u0060worker\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You are a workbook worker. tools=[] means never CALL. Finish the task with a direct final answer; do not invent tools. \u002a\n","s4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","s5":"} \u002a\n","s6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","s7":"\u002a\u0060out\u0060 = > \u0060worker\u0060.step task=\u0060task\u0060 stream=True \u002a\n","s7b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","s8":"\u002a\u002a\u0060text\u0060\u002a\u002a\n","d3":"\u002a\u0060research\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You gather facts for the goal. Be concise. Do not invent tools. \u002a\n","d3b":"\u002a\u0060writer\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You write the final answer from research notes. Do not invent tools. \u002a\n","d4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","d5":"} \u002a\n","d6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","d7":"\u002a\u0060notes\u0060 = > \u0060research\u0060.step task=\u0060task\u0060 stream=True \u002a\n","d8":"\u002a\u0060ns\u0060 = > json.stringify value=\u0060notes\u0060 \u002a\n","d9":"\u002a\u0060wt\u0060 = Write the final answer. Research notes: \u002a\n","d10":"\u002a\u0060wt\u0060 = \u0060wt\u0060 + \u0060ns\u0060 \u002a\n","d11":"\u002a\u0060out\u0060 = > \u0060writer\u0060.step task=\u0060wt\u0060 stream=True \u002a\n","d11b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","d12":"\u002a\u002a\u0060text\u0060\u002a\u002a\n"}*
*h = > json.get value=`esc` key="h"* *h = > json.get value=`esc` key="h"*
*s1 = > json.get value=`esc` key="s1"* *s1 = > json.get value=`esc` key="s1"*
*s2 = > json.get value=`esc` key="s2"* *s2 = > json.get value=`esc` key="s2"*
@@ -171,7 +171,7 @@ Build a runnable workbook. `skeleton=single` (default): one worker `step`. `skel
Structured observation for the parent developer-agent: source, named writeback slots, exit code, optional child return value, and quiet-captured I/O. Structured observation for the parent developer-agent: source, named writeback slots, exit code, optional child return value, and quiet-captured I/O.
*source = > fs.read_text path=`path`* *source = > fs.read_text path=`path`*
*slots = > writeback.scan_path path=`path`* *slots = > wb.scan_path path=`path`*
*obs = > json.parse text={"path":""}* *obs = > json.parse text={"path":""}*
*obs = > json.set map=`obs` key="path" value=`path`* *obs = > json.set map=`obs` key="path" value=`path`*
*obs = > json.set map=`obs` key="source" value=`source`* *obs = > json.set map=`obs` key="source" value=`source`*
@@ -1463,7 +1463,7 @@ Always append `done` and attach `events` on the result map (process audit even w
1. `trace` 1. `trace`
*body = > json.stringify value=`events`* *body = > json.stringify value=`events`*
> host_writeback_record value=`body` key="trace" > wb.record value=`body` key="trace"
2. * 2. *
*_ = 1* *_ = 1*
@@ -1606,9 +1606,9 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
*st = > json.get value=`out` key="status"* *st = > json.get value=`out` key="status"*
1. `st` == "ok" 1. `st` == "ok"
> host_writeback_record value=`body` key="ok" > wb.record value=`body` key="ok"
2. * 2. *
> host_writeback_record value=`body` key="error" > wb.record value=`body` key="error"
2. * 2. *
*_ = 1* *_ = 1*
@@ -1711,7 +1711,7 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near`
*events = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` result=`child_val` stream=`stream` echo=`echo`* *events = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` result=`child_val` stream=`stream` echo=`echo`*
1. `writeback` 1. `writeback`
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
> host_writeback_record value=`body` key="ok" > wb.record value=`body` key="ok"
2. * 2. *
*_ = 1* *_ = 1*
*out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val`* *out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val`*
@@ -1768,7 +1768,7 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near`
*events = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` result=`child_val` stream=`stream` echo=`echo`* *events = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` result=`child_val` stream=`stream` echo=`echo`*
1. `writeback` 1. `writeback`
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
> host_writeback_record value=`body` key="ok" > wb.record value=`body` key="ok"
2. * 2. *
*_ = 1* *_ = 1*
*out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val`* *out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val`*
@@ -1832,7 +1832,7 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near`
*events = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` result=`child_val` stream=`stream` echo=`echo`* *events = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` result=`child_val` stream=`stream` echo=`echo`*
1. `writeback` 1. `writeback`
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
> host_writeback_record value=`body` key="ok" > wb.record value=`body` key="ok"
2. * 2. *
*_ = 1* *_ = 1*
*out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val`* *out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val`*
@@ -1910,7 +1910,7 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near`
*out = > json.set map=`out` key="cache" value="bypass"* *out = > json.set map=`out` key="cache" value="bypass"*
1. `writeback` 1. `writeback`
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
> host_writeback_record value=`body` key="ok" > wb.record value=`body` key="ok"
2. * 2. *
*_ = 1* *_ = 1*
*out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace`* *out = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace`*
@@ -2091,9 +2091,9 @@ Deterministic success stop (loop engineering): when the child already returned a
1. `writeback` 1. `writeback`
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
1. `status` == "ok" 1. `status` == "ok"
> host_writeback_record value=`body` key="ok" > wb.record value=`body` key="ok"
2. * 2. *
> host_writeback_record value=`body` key="error" > wb.record value=`body` key="error"
2. * 2. *
*_ = 1* *_ = 1*
+2 -4
View File
@@ -17,7 +17,7 @@ Load `.env` from cwd (optional named arg `path=`). Does not override existing va
## stream_result ## stream_result
+ `events` + `events`
Reduce a `complete stream=True` event list to the final `done.result` text (or `error.message`). Unknown event types abort. Reduce a `complete stream=True` event list to the final `done.result` text (or `error.message`). Unknown types (e.g. `reasoning`) are ignored.
*answer = ""* *answer = ""*
@@ -29,9 +29,7 @@ Reduce a `complete stream=True` event list to the final `done.result` text (or `
*answer = ev[^message]* *answer = ev[^message]*
3. `t` == "delta" 3. `t` == "delta"
4. * 4. *
> print text=ext/ai/llm: unexpected SSE event type *_ = 1*
> print text=`t`
> sys.exit code=1
**answer** **answer**
+2 -4
View File
@@ -17,7 +17,7 @@ import json:lib/json.mq.md
## 流式结果 ## 流式结果
+ `事件` + `事件`
把 `运行 流式=真` 的事件列表收成最终 `done.result`(或 `error.message`)。未知事件类型直接失败。 把 `运行 流式=真` 的事件列表收成最终 `done.result`(或 `error.message`)。未知类型(如 `reasoning`)忽略。
*answer = ""* *answer = ""*
@@ -29,9 +29,7 @@ import json:lib/json.mq.md
*answer = ev[^message]* *answer = ev[^message]*
3. `t` == "delta" 3. `t` == "delta"
4. * 4. *
> 打印 内容=ext/ai/大模型: 未预期的 SSE 事件类型 *_ = 1*
> 打印 内容=`t`
> 系统.退出 码=1
**answer** **answer**
+2 -2
View File
@@ -175,9 +175,9 @@ import json:lib/json.mq.md
*body = > json.stringify value=`out`* *body = > json.stringify value=`out`*
*st = > json.get value=`out` key="status"* *st = > json.get value=`out` key="status"*
1. `st` == "ok" 1. `st` == "ok"
> host_writeback_record value=`body` key="ok" > 自写回.写回 值=`body` 键="ok"
2. * 2. *
> host_writeback_record value=`body` key="error" > 自写回.写回 值=`body` 键="error"
2. * 2. *
*_ = 1* *_ = 1*
+372
View File
@@ -0,0 +1,372 @@
---
title: ext/linalg/linalg
description: Formula-first linear algebra (English). MatExpr simplify + display; ABI plugin.
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
## ensure_plugin
Load the ABI v2 `linalg` plugin once.
*p = > plugin.native_path name="linalg"*
1. `p`
> plugin.load path=`p`
2. *
> print text=ext/linalg: native linalg plugin not found (build marqdo_plugin_linalg or marqdo ext add linalg)
> sys.exit code=1
****
## ping
> ensure_plugin
**> linalg_ping**
## mul
+ `a`
+ `b`
Matrix product (lazy MatMul; shape-checked).
> ensure_plugin
**> linalg_mul a=`a` b=`b`**
## add
+ `a`
+ `b`
> ensure_plugin
**> linalg_add a=`a` b=`b`**
## sub
+ `a`
+ `b`
> ensure_plugin
**> linalg_sub a=`a` b=`b`**
## transpose
+ `expr`
> ensure_plugin
**> linalg_transpose expr=`expr`**
## inv
+ `expr`
Symbolic inverse (lazy).
> ensure_plugin
**> linalg_inv expr=`expr`**
## simplify
+ `expr`
Apply MatExpr rewrite rules (R1–R5).
> ensure_plugin
**> linalg_simplify expr=`expr`**
## ascii
+ `expr`
+ `raw`=False
Display ASCII (default **simplify** first). Pass `raw=True` to keep the written tree.
> ensure_plugin
**> linalg_ascii expr=`expr` raw=`raw`**
## latex
+ `expr`
+ `raw`=False
> ensure_plugin
**> linalg_latex expr=`expr` raw=`raw`**
## shape
+ `expr`
> ensure_plugin
**> linalg_shape expr=`expr`**
## show
+ `expr`
+ `path`=None
+ `raw`=False
Record a formula SVG via host plot channel (default simplify). Returns the expr map.
> ensure_plugin
**> linalg_show expr=`expr` path=`path` raw=`raw`**
## eye
+ `n`
> ensure_plugin
**> linalg_eye n=`n`**
## zeros
+ `rows`
+ `cols`
> ensure_plugin
**> linalg_zeros rows=`rows` cols=`cols`**
## from_list
+ `data`
Dense leaf from nested list.
> ensure_plugin
**> linalg_from_list data=`data`**
## from_formula
+ `formula`
Absorb a `$$` numeric matrix formula (or nested list / dense map) as a dense leaf.
> ensure_plugin
**> linalg_from_formula formula=`formula`**
## explicit
+ `expr`
+ `raw`=False
Evaluate to dense (default **simplify** first).
> ensure_plugin
**> linalg_explicit expr=`expr` raw=`raw`**
## det
+ `expr`
> ensure_plugin
**> linalg_det expr=`expr`**
## trace
+ `expr`
> ensure_plugin
**> linalg_trace expr=`expr`**
## solve
+ `a`=None
+ `b`
+ `factor`=None
Solve `a x = b` (Gaussian elimination), or reuse `factorize kind=lu` via `factor` + `b`.
> ensure_plugin
**> linalg_solve a=`a` b=`b` factor=`factor`**
## matmul
+ `a`
+ `b`
Dense matrix product → `linalg_dense`.
> ensure_plugin
**> linalg_matmul a=`a` b=`b`**
## kron
+ `a`
+ `b`
Lazy Kronecker product (does not expand).
> ensure_plugin
**> linalg_kron a=`a` b=`b`**
## block
+ `blocks`
Block matrix from a nested list of expressions.
> ensure_plugin
**> linalg_block blocks=`blocks`**
## collapse
+ `expr`
Block-level collapse (`Block*Block` → block of products).
> ensure_plugin
**> linalg_collapse expr=`expr`**
## factorize
+ `matrix`
+ `kind`="lu"
Dense factorization → `linalg_factor` (`kind` = `lu` | `qr` | `svd` | `eig` | `chol`).
> ensure_plugin
**> linalg_factorize matrix=`matrix` kind=`kind`**
## draw
+ `factor`
+ `kind`=None
+ `path`=None
+ `theme`="light"
Structure SVG (`eig` | `svd` | `qr` | `lu` | `ge` | `chol` | `structure` | `heatmap` | `hinton`). Theme tokens match quantum Q8: `light` | `dark` | `bw`.
> ensure_plugin
**> linalg_draw factor=`factor` kind=`kind` path=`path` theme=`theme`**
## lstsq
+ `a`
+ `b`
Least squares `min ||a x - b||` (QR if tall, SVD if wide).
> ensure_plugin
**> linalg_lstsq a=`a` b=`b`**
## norm
+ `expr`
+ `ord`="fro"
Matrix norm: `fro` | `1` | `inf` | `2`. Complex dense supports `fro` only.
> ensure_plugin
**> linalg_norm expr=`expr` ord=`ord`**
## cond
+ `expr`
Spectral condition number via SVD (`ord=2`).
> ensure_plugin
**> linalg_cond expr=`expr`**
## rank
+ `expr`
Numerical rank (SVD).
> ensure_plugin
**> linalg_rank expr=`expr`**
## declare
+ `table`
Declare several matrix symbols from an `@` record table (`name` / `rows` / `cols`). Returns a map env.
> ensure_plugin
**> linalg_declare table=`table` type_tag="matrix"**
## symbol
+ `name`
+ `rows`
+ `cols`
Abstract matrix symbol. Returns a `# matrix` instance (supports `+` / `*` and methods).
> ensure_plugin
**> matrix name=`name` rows=`rows` cols=`cols`**
# matrix
+ `name`=None
+ `rows`=None
+ `cols`=None
+ `data`=None
+ `formula`=None
+ `n`=None
Matrix value (`_type=matrix`). Prefer infix `A + B` / `A * B`, or methods `` `A`.T ``.
> ensure_plugin
1. `formula`
**> linalg_from_formula formula=`formula`**
2. `data`
**> linalg_from_list data=`data`**
3. `name`
**> linalg_symbol name=`name` rows=`rows` cols=`cols`**
4. `n`
**> linalg_eye n=`n`**
5. `rows`
**> linalg_zeros rows=`rows` cols=`cols`**
6. *
> print text=ext/linalg: matrix needs name= / data= / formula= / n= / rows=
**None**
## T
**> linalg_transpose expr=`self`**
## T_ascii
Transpose then display ASCII (auto-simplified). Handy for lecture identities.
*`Pt` = > linalg_transpose expr=`self`*
**> linalg_ascii expr=`Pt` raw=False**
## add
+ `b`
**> linalg_add a=`self` b=`b`**
## sub
+ `b`
**> linalg_sub a=`self` b=`b`**
## mul
+ `b`
**> linalg_mul a=`self` b=`b`**
## inv
**> linalg_inv expr=`self`**
## simplify
**> linalg_simplify expr=`self`**
## ascii
**> linalg_ascii expr=`self` raw=False**
## latex
**> linalg_latex expr=`self` raw=False**
## explicit
**> linalg_explicit expr=`self` raw=False**
## det
**> linalg_det expr=`self`**
## trace
**> linalg_trace expr=`self`**
## show
+ `path`=None
+ `raw`=False
**> linalg_show expr=`self` path=`path` raw=`raw`**
## eye
+ `n`
**> linalg_eye n=`n`**
## zeros
+ `rows`
+ `cols`
**> linalg_zeros rows=`rows` cols=`cols`**
## from_list
+ `data`
**> linalg_from_list data=`data`**
## from_formula
+ `formula`
**> linalg_from_formula formula=`formula`**
+367
View File
@@ -0,0 +1,367 @@
---
title: ext/linalg/线性代数
description: 公式优先的线性代数(中文)。矩阵表达式化简与展示;ABI 插件。
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
## 确保插件
加载 ABI v2 `linalg` 插件一次。
*p = > plugin.native_path name="linalg"*
1. `p`
> plugin.load path=`p`
2. *
> print text=ext/linalg: 未找到原生 linalg 插件(请 build marqdo_plugin_linalg 或 marqdo ext add linalg)
> sys.exit code=1
****
## 探测
> 确保插件
**> linalg_ping**
## 乘
+ `左`
+ `右`
矩阵乘(惰性;检查形状)。
> 确保插件
**> linalg_mul a=`左` b=`右`**
## 加
+ `左`
+ `右`
> 确保插件
**> linalg_add a=`左` b=`右`**
## 减
+ `左`
+ `右`
> 确保插件
**> linalg_sub a=`左` b=`右`**
## 转置
+ `式`
> 确保插件
**> linalg_transpose expr=`式`**
## 逆
+ `式`
> 确保插件
**> linalg_inv expr=`式`**
## 化简
+ `式`
应用矩阵表达式重写规则(R1–R5)。
> 确保插件
**> linalg_simplify expr=`式`**
## 文本
+ `式`
+ `原始`=False
展示 ASCII(默认先 **化简**)。`原始=True` 保留书写树。
> 确保插件
**> linalg_ascii expr=`式` raw=`原始`**
## 乳胶
+ `式`
+ `原始`=False
> 确保插件
**> linalg_latex expr=`式` raw=`原始`**
## 形状
+ `式`
> 确保插件
**> linalg_shape expr=`式`**
## 展示
+ `式`
+ `路径`=None
+ `原始`=False
写入公式 SVG(默认化简)。
> 确保插件
**> linalg_show expr=`式` path=`路径` raw=`原始`**
## 单位
+ `阶`
> 确保插件
**> linalg_eye n=`阶`**
## 零阵
+ `行`
+ `列`
> 确保插件
**> linalg_zeros rows=`行` cols=`列`**
## 自列表
+ `数据`
> 确保插件
**> linalg_from_list data=`数据`**
## 自公式
+ `公式`
吸收 `$$` 数值矩阵公式为稠密叶。
> 确保插件
**> linalg_from_formula formula=`公式`**
## 求值
+ `式`
+ `原始`=False
求成稠密(默认先 **化简**)。
> 确保插件
**> linalg_explicit expr=`式` raw=`原始`**
## 行列式
+ `式`
> 确保插件
**> linalg_det expr=`式`**
## 迹
+ `式`
> 确保插件
**> linalg_trace expr=`式`**
## 求解
+ `左`=None
+ `右`
+ `分解`=None
解 `左 · x = 右`,或复用 `分解`(`kind=lu`)+ `右`。
> 确保插件
**> linalg_solve a=`左` b=`右` factor=`分解`**
## 稠密乘
+ `左`
+ `右`
> 确保插件
**> linalg_matmul a=`左` b=`右`**
## 克罗内克
+ `左`
+ `右`
惰性 Kronecker 积(默认不展开)。
> 确保插件
**> linalg_kron a=`左` b=`右`**
## 分块
+ `块`
由嵌套表达式列表构造分块矩阵。
> 确保插件
**> linalg_block blocks=`块`**
## 分块化简
+ `式`
分块级塌缩(`Block*Block` → 块乘加)。
> 确保插件
**> linalg_collapse expr=`式`**
## 分解
+ `矩阵`
+ `类型`="lu"
稠密分解 → `linalg_factor`(`类型` = `lu` | `qr` | `svd` | `eig` | `chol`)。
> 确保插件
**> linalg_factorize matrix=`矩阵` kind=`类型`**
## 绘图
+ `分解`
+ `类型`=None
+ `路径`=None
+ `主题`="light"
结构 SVG(`eig` | `svd` | `qr` | `lu` | `ge` | `chol` | `structure` | `heatmap` | `hinton`)。主题与 quantum Q8 对齐:`light` | `dark` | `bw`。
> 确保插件
**> linalg_draw factor=`分解` kind=`类型` path=`路径` theme=`主题`**
## 最小二乘
+ `左`
+ `右`
最小二乘 `min ||左 · x - 右||`(高瘦用 QR,宽用 SVD)。
> 确保插件
**> linalg_lstsq a=`左` b=`右`**
## 范数
+ `式`
+ `范数`="fro"
矩阵范数:`fro` | `1` | `inf` | `2`。复数稠密暂仅 `fro`。
> 确保插件
**> linalg_norm expr=`式` ord=`范数`**
## 条件数
+ `式`
谱条件数(SVD,`ord=2`)。
> 确保插件
**> linalg_cond expr=`式`**
## 秩
+ `式`
数值秩(SVD)。
> 确保插件
**> linalg_rank expr=`式`**
## 声明
+ `表`
用 `@` 记录表一次声明多个符号矩阵(`名` / `行` / `列`)。返回环境 map。
> 确保插件
**> linalg_declare table=`表` type_tag="矩阵"**
## 符号
+ `名`
+ `行`
+ `列`
抽象矩阵符号。返回 `# 矩阵` 实例(可用 `+` / `*` 与方法)。
> 确保插件
**> 矩阵 名=`名` 行=`行` 列=`列`**
# 矩阵
+ `名`=None
+ `行`=None
+ `列`=None
+ `数据`=None
+ `公式`=None
+ `阶`=None
矩阵值(`_type=矩阵`)。优先中缀 `A + B` / `A * B`,或 `` `A`.转置 ``。
> 确保插件
1. `公式`
**> linalg_from_formula formula=`公式`**
2. `数据`
**> linalg_from_list data=`数据`**
3. `名`
**> linalg_symbol name=`名` rows=`行` cols=`列`**
4. `阶`
**> linalg_eye n=`阶`**
5. `行`
**> linalg_zeros rows=`行` cols=`列`**
6. *
> print text=ext/linalg: 矩阵需要 名= / 数据= / 公式= / 阶= / 行=
**None**
## 转置
**> linalg_transpose expr=`self`**
## 转置文本
转置后输出 ASCII(默认化简)。适合讲义恒等式验收。
*`转` = > linalg_transpose expr=`self`*
**> linalg_ascii expr=`转` raw=False**
## 加
+ `右`
**> linalg_add a=`self` b=`右`**
## 减
+ `右`
**> linalg_sub a=`self` b=`右`**
## 乘
+ `右`
**> linalg_mul a=`self` b=`右`**
## 逆
**> linalg_inv expr=`self`**
## 化简
**> linalg_simplify expr=`self`**
## 文本
**> linalg_ascii expr=`self` raw=False**
## 乳胶
**> linalg_latex expr=`self` raw=False**
## 求值
+ `原始`=False
**> linalg_explicit expr=`self` raw=`原始`**
## 行列式
**> linalg_det expr=`self`**
## 迹
**> linalg_trace expr=`self`**
## 展示
+ `路径`=None
+ `原始`=False
**> linalg_show expr=`self` path=`路径` raw=`原始`**
## 单位
+ `阶`
**> linalg_eye n=`阶`**
## 零阵
+ `行`
+ `列`
**> linalg_zeros rows=`行` cols=`列`**
## 自列表
+ `数据`
**> linalg_from_list data=`数据`**
## 自公式
+ `公式`
**> linalg_from_formula formula=`公式`**
+11
View File
@@ -0,0 +1,11 @@
---
title: lib/cli
description: Parse argv flags (Mid M5). --key value / --key=value / --flag; positionals under _.
---
## parse
+ `args`=None
Parse `--key value` / `--key=value` / `--flag`. A bare `--flag` must not be followed by a non-option token if you need it as boolean (put flags last, or use `--flag=true`). Positionals land under `_`.
**> host_cli_parse args=`args`**
+14
View File
@@ -0,0 +1,14 @@
---
title: lib/csv
description: RFC4180-ish CSV parse/stringify (Mid M4). First row = headers → list of maps.
---
## parse
+ `text`
**> host_csv_parse text=`text`**
## stringify
+ `rows`
**> host_csv_stringify rows=`rows`**
+24
View File
@@ -0,0 +1,24 @@
---
title: lib/encoding
description: Base64 and hex encode/decode (Mid M1). UTF-8 text round-trip.
---
## base64_encode
+ `text`
**> host_encoding_base64_encode text=`text`**
## base64_decode
+ `text`
**> host_encoding_base64_decode text=`text`**
## hex_encode
+ `text`
**> host_encoding_hex_encode text=`text`**
## hex_decode
+ `text`
**> host_encoding_hex_decode text=`text`**
+23
View File
@@ -40,6 +40,29 @@ description: English filesystem wrappers
**> host_remove path=`path`** **> host_remove path=`path`**
## copy_file
+ `src`
+ `dest`
Copy a file (overwrites `dest` if it exists). Paths are sandboxed like other fs ops.
**> host_copy_file src=`src` dest=`dest`**
## move
+ `src`
+ `dest`
Rename / move within the sandbox.
**> host_move src=`src` dest=`dest`**
## make_temp
+ `prefix`=None
Create an empty temp file under the program directory; return its relative path.
**> host_make_temp prefix=`prefix`**
## text_patch ## text_patch
+ `path` + `path`
+ `find` + `find`
+25
View File
@@ -0,0 +1,25 @@
---
title: lib/hash
description: Digests and HMAC (Mid M3). Integrity / cache keys — not password hashing.
---
## sha256
+ `text`
**> host_hash_sha256 text=`text`**
## sha1
+ `text`
**> host_hash_sha1 text=`text`**
## md5
+ `text`
**> host_hash_md5 text=`text`**
## hmac_sha256
+ `key`
+ `text`
**> host_hash_hmac_sha256 key=`key` text=`text`**
+47
View File
@@ -0,0 +1,47 @@
---
title: lib/log
description: Level-filtered one-line logs (Mid M6). Default level info.
---
## set_level
+ `level`
level is debug / info / warn / error.
**> host_log_set_level level=`level`**
## debug
+ `text`
*line = > host_log_line level="debug" text=`text`*
1. `line`
> print text=`line`
2. *
****
## info
+ `text`
*line = > host_log_line level="info" text=`text`*
1. `line`
> print text=`line`
2. *
****
## warn
+ `text`
*line = > host_log_line level="warn" text=`text`*
1. `line`
> print text=`line`
2. *
****
## error
+ `text`
*line = > host_log_line level="error" text=`text`*
1. `line`
> print text=`line`
2. *
****
+40
View File
@@ -0,0 +1,40 @@
---
title: lib/path
description: Path algebra (Mid M2). No I/O; OS separators.
---
## join
+ `a`
+ `b`
**> host_path_join a=`a` b=`b`**
## split
+ `path`
**> host_path_split path=`path`**
## file_name
+ `path`
**> host_path_file_name path=`path`**
## parent
+ `path`
**> host_path_parent path=`path`**
## extension
+ `path`
**> host_path_extension path=`path`**
## normalize
+ `path`
**> host_path_normalize path=`path`**
## is_absolute
+ `path`
**> host_path_is_absolute path=`path`**
+38
View File
@@ -0,0 +1,38 @@
---
title: lib/re
description: Regular expressions (Mid M1). Unicode; no backreferences.
---
## is_match
+ `text`
+ `pattern`
**> host_re_is_match text=`text` pattern=`pattern`**
## find
+ `text`
+ `pattern`
**> host_re_find text=`text` pattern=`pattern`**
## find_all
+ `text`
+ `pattern`
**> host_re_find_all text=`text` pattern=`pattern`**
## replace
+ `text`
+ `pattern`
+ `with`
+ `count`=None
Optional `count=` limits replacements (`None` = all).
**> host_re_replace text=`text` pattern=`pattern` with=`with` count=`count`**
## split
+ `text`
+ `pattern`
**> host_re_split text=`text` pattern=`pattern`**
+18
View File
@@ -0,0 +1,18 @@
---
title: lib/secrets
description: Cryptographic tokens (Mid M3). Do not use math.random for secrets.
---
## token_hex
+ `n`=None
Byte length `n` (default 16). Returns lowercase hex of length `2*n`.
**> host_secrets_token_hex n=`n`**
## token_urlsafe
+ `n`=None
Byte length `n` (default 16). URL-safe Base64 without padding.
**> host_secrets_token_urlsafe n=`n`**
+43
View File
@@ -129,6 +129,49 @@ Zero-based index or `None`.
**> host_list_reverse list=`list`** **> host_list_reverse list=`list`**
## sort
+ `list`
Ascending sort (int/num/text preferred).
**> host_list_sort list=`list`**
## sort_by
+ `list`
+ `key`
Sort a list of maps by `key`.
**> host_list_sort_by list=`list` key=`key`**
## unique
+ `list`
Stable unique (first wins).
**> host_list_unique list=`list`**
## chunk
+ `list`
+ `size`
**> host_list_chunk list=`list` size=`size`**
## zip
+ `a`
+ `b`
Pairs until the shorter list ends.
**> host_list_zip a=`a` b=`b`**
## flatten
+ `list`
One-level flatten.
**> host_list_flatten list=`list`**
## clear ## clear
+ `value` + `value`
+55 -1
View File
@@ -1,6 +1,6 @@
--- ---
title: lib/text title: lib/text
description: English text helpers over trim / split / join description: Text helpers (Mid M4 thicken). Literal replace — use lib/re for regex.
--- ---
## str_trim ## str_trim
@@ -19,3 +19,57 @@ description: English text helpers over trim / split / join
+ `sep` + `sep`
**> join value=`xs` sep=`sep`** **> join value=`xs` sep=`sep`**
## contains
+ `text`
+ `sub`
**> host_text_contains text=`text` sub=`sub`**
## starts_with
+ `text`
+ `prefix`
**> host_text_starts_with text=`text` prefix=`prefix`**
## ends_with
+ `text`
+ `suffix`
**> host_text_ends_with text=`text` suffix=`suffix`**
## replace
+ `text`
+ `old`
+ `new`
+ `count`=None
Literal substring replace (`count` optional).
**> host_text_replace text=`text` old=`old` new=`new` count=`count`**
## to_upper
+ `text`
**> host_text_to_upper text=`text`**
## to_lower
+ `text`
**> host_text_to_lower text=`text`**
## repeat
+ `text`
+ `n`
**> host_text_repeat text=`text` n=`n`**
## pad
+ `text`
+ `width`
+ `fill`=None
+ `align`=None
align: left (default), right, or center.
**> host_text_pad text=`text` width=`width` fill=`fill` align=`align`**
+8
View File
@@ -0,0 +1,8 @@
---
title: lib/uuid
description: UUID v4 (Mid M6). Cryptographic random via getrandom.
---
## v4
**> host_uuid_v4**
+11
View File
@@ -0,0 +1,11 @@
---
title: lib/命令行
description: 解析命令行参数(中层 M5)。--键 值 / --键=值 / --开关;位置参在 _。
---
## 解析
+ `参数`=None
省略 `参数` 时用进程 argv(系统.参数表)。
**> host_cli_parse args=`参数`**
+25
View File
@@ -0,0 +1,25 @@
---
title: lib/哈希
description: 摘要与 HMAC(中层 M3)。完整性 / 缓存键 — 非密码存储。
---
## 哈希256
+ `内容`
**> host_hash_sha256 text=`内容`**
## 哈希1
+ `内容`
**> host_hash_sha1 text=`内容`**
## md5
+ `内容`
**> host_hash_md5 text=`内容`**
## 密钥哈希256
+ `密钥`
+ `内容`
**> host_hash_hmac_sha256 key=`密钥` text=`内容`**
+23
View File
@@ -40,6 +40,29 @@ description: 中文文件系统包装
**> host_remove path=`路径`** **> host_remove path=`路径`**
## 复制文件
+ `源`
+ `目标`
复制文件(目标已存在则覆盖)。路径沙箱同其他文件操作。
**> host_copy_file src=`源` dest=`目标`**
## 移动
+ `源`
+ `目标`
在沙箱内重命名 / 移动。
**> host_move src=`源` dest=`目标`**
## 建临时
+ `前缀`=None
在程序目录下创建空临时文件,返回相对路径。
**> host_make_temp prefix=`前缀`**
## 文本补丁 ## 文本补丁
+ `路径` + `路径`
+ `查找` + `查找`
+55 -1
View File
@@ -1,6 +1,6 @@
--- ---
title: lib/文本 title: lib/文本
description: 中文文本库:去空白 / 拆分 / 拼接 description: 文本库加厚(中层 M4)。字面替换 — 正则请用 lib/正则。
--- ---
## 去空白 ## 去空白
@@ -19,3 +19,57 @@ description: 中文文本库:去空白 / 拆分 / 拼接
+ `sep` + `sep`
**> join value=`xs` sep=`sep`** **> join value=`xs` sep=`sep`**
## 包含
+ `内容`
+ `子串`
**> host_text_contains text=`内容` sub=`子串`**
## 开头是
+ `内容`
+ `前缀`
**> host_text_starts_with text=`内容` prefix=`前缀`**
## 结尾是
+ `内容`
+ `后缀`
**> host_text_ends_with text=`内容` suffix=`后缀`**
## 替换
+ `内容`
+ `旧`
+ `新`
+ `次数`=None
字面子串替换(可选 `次数`)。
**> host_text_replace text=`内容` old=`旧` new=`新` count=`次数`**
## 大写
+ `内容`
**> host_text_to_upper text=`内容`**
## 小写
+ `内容`
**> host_text_to_lower text=`内容`**
## 重复
+ `内容`
+ `次数`
**> host_text_repeat text=`内容` n=`次数`**
## 填充
+ `内容`
+ `宽度`
+ `填充符`=None
+ `对齐`=None
对齐:left(默认)/ right / center。
**> host_text_pad text=`内容` width=`宽度` fill=`填充符` align=`对齐`**
+47
View File
@@ -0,0 +1,47 @@
---
title: lib/日志
description: 按级别过滤的一行日志(中层 M6)。默认级别 info。
---
## 设级别
+ `级别`
级别:debug / info / warn / error(或 调试/信息/警告/错误)。
**> host_log_set_level level=`级别`**
## 调试
+ `内容`
*line = > host_log_line level="debug" text=`内容`*
1. `line`
> 打印 内容=`line`
2. *
****
## 信息
+ `内容`
*line = > host_log_line level="info" text=`内容`*
1. `line`
> 打印 内容=`line`
2. *
****
## 警告
+ `内容`
*line = > host_log_line level="warn" text=`内容`*
1. `line`
> 打印 内容=`line`
2. *
****
## 错误
+ `内容`
*line = > host_log_line level="error" text=`内容`*
1. `line`
> 打印 内容=`line`
2. *
****
+18
View File
@@ -0,0 +1,18 @@
---
title: lib/机密
description: 安全随机令牌(中层 M3)。勿用 math.random 生成令牌。
---
## 十六进制令牌
+ `字节数`=None
字节长度(默认 16),返回长度为 `2*字节数` 的小写 hex。
**> host_secrets_token_hex n=`字节数`**
## 网址安全令牌
+ `字节数`=None
字节长度(默认 16),URL-safe Base64(无填充)。
**> host_secrets_token_urlsafe n=`字节数`**
+8
View File
@@ -0,0 +1,8 @@
---
title: lib/标识
description: UUID 版本 4(中层 M6)。经 getrandom 安全随机。
---
## 版本4
**> host_uuid_v4**
+38
View File
@@ -0,0 +1,38 @@
---
title: lib/正则
description: 正则表达式(中层 M1)。Unicode;无回溯引用。
---
## 匹配
+ `内容`
+ `模式`
**> host_re_is_match text=`内容` pattern=`模式`**
## 查找
+ `内容`
+ `模式`
**> host_re_find text=`内容` pattern=`模式`**
## 查找全部
+ `内容`
+ `模式`
**> host_re_find_all text=`内容` pattern=`模式`**
## 替换
+ `内容`
+ `模式`
+ `为`
+ `次数`=None
可选 `次数=` 限制替换次数(缺省 = 全部)。
**> host_re_replace text=`内容` pattern=`模式` with=`为` count=`次数`**
## 拆分
+ `内容`
+ `模式`
**> host_re_split text=`内容` pattern=`模式`**
+24
View File
@@ -0,0 +1,24 @@
---
title: lib/编码
description: Base64 与十六进制编解码(中层 M1)。UTF-8 文本往返。
---
## 基64编码
+ `内容`
**> host_encoding_base64_encode text=`内容`**
## 基64解码
+ `内容`
**> host_encoding_base64_decode text=`内容`**
## 十六进制编码
+ `内容`
**> host_encoding_hex_encode text=`内容`**
## 十六进制解码
+ `内容`
**> host_encoding_hex_decode text=`内容`**
+43
View File
@@ -129,6 +129,49 @@ description: >-
**> host_list_reverse list=`list`** **> host_list_reverse list=`list`**
## 排序
+ `list`
升序(优先 int/num/text)。
**> host_list_sort list=`list`**
## 按键排序
+ `list`
+ `key`
对 map 列表按键排序。
**> host_list_sort_by list=`list` key=`key`**
## 去重
+ `list`
保序去重(保留首次)。
**> host_list_unique list=`list`**
## 分块
+ `list`
+ `size`
**> host_list_chunk list=`list` size=`size`**
## 拉链
+ `a`
+ `b`
取较短长度配对。
**> host_list_zip a=`a` b=`b`**
## 展平
+ `list`
一层展平。
**> host_list_flatten list=`list`**
## 清空 ## 清空
+ `value` + `value`
+40
View File
@@ -0,0 +1,40 @@
---
title: lib/路径
description: 路径代数(中层 M2)。无 I/O;分隔符随宿主 OS。
---
## 拼接
+ `甲`
+ `乙`
**> host_path_join a=`甲` b=`乙`**
## 拆分
+ `路径`
**> host_path_split path=`路径`**
## 文件名
+ `路径`
**> host_path_file_name path=`路径`**
## 父目录
+ `路径`
**> host_path_parent path=`路径`**
## 扩展名
+ `路径`
**> host_path_extension path=`路径`**
## 规范化
+ `路径`
**> host_path_normalize path=`路径`**
## 是绝对
+ `路径`
**> host_path_is_absolute path=`路径`**
+14
View File
@@ -0,0 +1,14 @@
---
title: lib/逗号表
description: CSV 解析/序列化(中层 M4)。首行为表头 → 映射列表。
---
## 解析
+ `内容`
**> host_csv_parse text=`内容`**
## 序列化
+ `行集`
**> host_csv_stringify rows=`行集`**
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "marqdo_plugin_linalg"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
description = "Marqdo linear algebra plugin — formula-first MatExpr (ABI v2)"
publish = false
[lib]
name = "linalg"
crate-type = ["cdylib"]
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+216
View File
@@ -0,0 +1,216 @@
//! Complex dense baseline (L6): parse/store + Frobenius norm.
//! Factorize / solve stay real-only for now.
use serde_json::{json, Value};
use crate::dense::MAX_ELEMENTS;
pub type CMat = Vec<Vec<(f64, f64)>>;
pub fn shape(m: &CMat) -> Result<(usize, usize), String> {
if m.is_empty() {
return Err("empty complex matrix".into());
}
let r = m.len();
let c = m[0].len();
if c == 0 || m.iter().any(|row| row.len() != c) {
return Err("complex matrix rows must have equal non-zero length".into());
}
Ok((r, c))
}
fn check_elems(rows: usize, cols: usize) -> Result<(), String> {
let n = rows.saturating_mul(cols);
if n > MAX_ELEMENTS {
return Err(format!(
"complex matrix {rows}×{cols} has {n} elements > limit {MAX_ELEMENTS}"
));
}
Ok(())
}
fn parse_cell(c: &Value) -> Result<(f64, f64), String> {
if let Some(n) = c
.as_f64()
.or_else(|| c.as_i64().map(|i| i as f64))
.or_else(|| c.as_u64().map(|u| u as f64))
{
return Ok((n, 0.0));
}
if let Some(arr) = c.as_array() {
if arr.len() == 2 {
let re = arr[0]
.as_f64()
.or_else(|| arr[0].as_i64().map(|i| i as f64))
.ok_or_else(|| "complex re must be number".to_string())?;
let im = arr[1]
.as_f64()
.or_else(|| arr[1].as_i64().map(|i| i as f64))
.ok_or_else(|| "complex im must be number".to_string())?;
return Ok((re, im));
}
}
if let Some(obj) = c.as_object() {
let re = obj
.get("re")
.or_else(|| obj.get("real"))
.and_then(|v| v.as_f64().or_else(|| v.as_i64().map(|i| i as f64)))
.ok_or_else(|| "complex cell needs re/real".to_string())?;
let im = obj
.get("im")
.or_else(|| obj.get("imag"))
.and_then(|v| v.as_f64().or_else(|| v.as_i64().map(|i| i as f64)))
.unwrap_or(0.0);
return Ok((re, im));
}
Err("complex cell must be number, [re,im], or {re,im}".into())
}
fn cell_looks_complex(c: &Value) -> bool {
if let Some(arr) = c.as_array() {
return arr.len() == 2
&& arr.iter().all(|x| {
x.is_number() || x.as_i64().is_some() || x.as_u64().is_some()
});
}
c.as_object()
.map(|o| o.contains_key("re") || o.contains_key("real") || o.contains_key("im"))
.unwrap_or(false)
}
/// True if nested list contains any complex-looking cell.
pub fn data_has_complex(v: &Value) -> bool {
let Some(rows) = v.as_array() else {
return false;
};
rows.iter().any(|row| {
row.as_array()
.map(|cells| cells.iter().any(cell_looks_complex))
.unwrap_or(false)
})
}
pub fn from_data(v: &Value) -> Result<CMat, String> {
let rows = v
.as_array()
.ok_or_else(|| "complex data must be list of rows".to_string())?;
let mut out = Vec::new();
let mut width = None;
for row in rows {
let cells = row
.as_array()
.ok_or_else(|| "complex row must be a list".to_string())?;
let mut r = Vec::new();
for c in cells {
r.push(parse_cell(c)?);
}
if let Some(w) = width {
if r.len() != w {
return Err("complex matrix rows must have equal length".into());
}
} else {
width = Some(r.len());
}
out.push(r);
}
let (rows, cols) = shape(&out)?;
check_elems(rows, cols)?;
Ok(out)
}
pub fn from_value(v: &Value) -> Result<CMat, String> {
if let Some(obj) = v.as_object() {
if obj.get("dtype").and_then(|d| d.as_str()) == Some("complex") {
if let Some(data) = obj.get("data") {
return from_data(data);
}
}
if let Some(data) = obj.get("data") {
if data_has_complex(data) {
return from_data(data);
}
}
}
if v.is_array() && data_has_complex(v) {
return from_data(v);
}
Err("expected linalg_dense dtype=complex or nested complex list".into())
}
fn format_c(z: (f64, f64)) -> String {
let (re, im) = z;
if im.abs() < 1e-15 {
if re.fract() == 0.0 && re.abs() < 1e15 {
format!("{}", re as i64)
} else {
format!("{re}")
}
} else if re.abs() < 1e-15 {
format!("{im}i")
} else {
format!("{re}{im:+}i")
}
}
pub fn to_dense_value(m: &CMat) -> Result<Value, String> {
let (rows, cols) = shape(m)?;
check_elems(rows, cols)?;
let data: Vec<Vec<Value>> = m
.iter()
.map(|row| {
row.iter()
.map(|&(re, im)| json!([re, im]))
.collect()
})
.collect();
let ascii_rows: Vec<String> = m
.iter()
.map(|row| {
let cells: Vec<String> = row.iter().map(|&z| format_c(z)).collect();
format!("[{}]", cells.join(","))
})
.collect();
Ok(json!({
"_type": "matrix",
"kind": "dense",
"rows": rows,
"cols": cols,
"data": data,
"dtype": "complex",
"ascii": format!("[{}]", ascii_rows.join(",")),
"latex": format!(
"\\begin{{bmatrix}}{}\\end{{bmatrix}}",
m.iter()
.map(|row| {
row.iter()
.map(|&z| format_c(z))
.collect::<Vec<_>>()
.join(" & ")
})
.collect::<Vec<_>>()
.join(" \\\\ ")
),
}))
}
pub fn frobenius_norm(m: &CMat) -> Result<f64, String> {
let _ = shape(m)?;
let mut s = 0.0;
for row in m {
for &(re, im) in row {
s += re * re + im * im;
}
}
Ok(s.sqrt())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fro_i() {
let m = from_data(&json!([[[0.0, 1.0], [1.0, 0.0]], [[1.0, 0.0], [0.0, -1.0]]])).unwrap();
assert!((frobenius_norm(&m).unwrap() - 2.0).abs() < 1e-12);
}
}
+583
View File
@@ -0,0 +1,583 @@
//! Dense real matrix kernels for L2 (`explicit` / solve / det / trace).
use serde_json::{json, Value};
use crate::matexpr::{Dim, Expr};
/// Max elements for `explicit` / dense ops (design default 10^4).
pub const MAX_ELEMENTS: usize = 10_000;
pub type Mat = Vec<Vec<f64>>;
pub fn check_elems(rows: usize, cols: usize) -> Result<(), String> {
let n = rows.saturating_mul(cols);
if n > MAX_ELEMENTS {
return Err(format!(
"dense matrix {rows}×{cols} has {n} elements > limit {MAX_ELEMENTS}; keep symbolic or reduce size"
));
}
Ok(())
}
pub fn eye(n: usize) -> Mat {
let mut m = vec![vec![0.0; n]; n];
for i in 0..n {
m[i][i] = 1.0;
}
m
}
pub fn zeros(rows: usize, cols: usize) -> Mat {
vec![vec![0.0; cols]; rows]
}
pub fn shape(m: &Mat) -> Result<(usize, usize), String> {
if m.is_empty() {
return Err("empty dense matrix".into());
}
let r = m.len();
let c = m[0].len();
if c == 0 || m.iter().any(|row| row.len() != c) {
return Err("dense matrix rows must have equal non-zero length".into());
}
Ok((r, c))
}
pub fn to_dense_value(m: &Mat) -> Result<Value, String> {
let (rows, cols) = shape(m)?;
check_elems(rows, cols)?;
Ok(json!({
"_type": "matrix",
"kind": "dense",
"rows": rows,
"cols": cols,
"data": m,
"dtype": "real",
"ascii": format_ascii(m),
"latex": format_latex(m),
}))
}
fn format_ascii(m: &Mat) -> String {
let rows: Vec<String> = m
.iter()
.map(|row| {
let cells: Vec<String> = row.iter().map(|x| format_num(*x)).collect();
format!("[{}]", cells.join(","))
})
.collect();
format!("[{}]", rows.join(","))
}
fn format_latex(m: &Mat) -> String {
let body: Vec<String> = m
.iter()
.map(|row| {
row.iter()
.map(|x| format_num(*x))
.collect::<Vec<_>>()
.join(" & ")
})
.collect();
format!(
"\\begin{{bmatrix}}{}\\end{{bmatrix}}",
body.join(" \\\\ ")
)
}
fn format_num(n: f64) -> String {
if n.fract() == 0.0 && n.abs() < 1e15 {
format!("{}", n as i64)
} else {
format!("{n}")
}
}
pub fn from_value(v: &Value) -> Result<Mat, String> {
if let Some(obj) = v.as_object() {
if let Some(data) = obj.get("data") {
return parse_data(data);
}
// linalg_expr kind=dense
if obj.get("kind").and_then(|k| k.as_str()) == Some("dense") {
if let Some(data) = obj.get("data") {
return parse_data(data);
}
}
}
// Formula Matrix / raw nested list
if v.is_array() {
return parse_data(v);
}
Err("expected matrix dense, dense expr, or nested list".into())
}
fn parse_data(v: &Value) -> Result<Mat, String> {
let rows = v
.as_array()
.ok_or_else(|| "dense data must be list of rows".to_string())?;
let mut out = Vec::new();
let mut width = None;
for row in rows {
let cells = row
.as_array()
.ok_or_else(|| "dense row must be a list".to_string())?;
let mut r = Vec::new();
for c in cells {
let n = c
.as_f64()
.or_else(|| c.as_i64().map(|i| i as f64))
.or_else(|| c.as_u64().map(|u| u as f64))
.or_else(|| c.as_str().and_then(|s| s.trim().parse().ok()))
.ok_or_else(|| "dense cell must be a number".to_string())?;
r.push(n);
}
if let Some(w) = width {
if r.len() != w {
return Err("dense matrix rows must have equal length".into());
}
} else {
width = Some(r.len());
}
out.push(r);
}
let (rows, cols) = shape(&out)?;
check_elems(rows, cols)?;
Ok(out)
}
fn dim_usize(d: &Dim) -> Result<usize, String> {
match d {
Dim::Int(n) => Ok(*n),
Dim::Sym(s) => Err(format!(
"cannot explicit symbolic dim `{s}`; bind concrete sizes or use dense leaves only"
)),
}
}
/// Evaluate MatExpr to dense; symbols are not allowed unless already dense leaves.
pub fn explicit(expr: &Expr) -> Result<Mat, String> {
match expr {
Expr::Dense { data } => {
let (r, c) = shape(data)?;
check_elems(r, c)?;
Ok(data.clone())
}
Expr::Eye { n } => {
let n = dim_usize(n)?;
check_elems(n, n)?;
Ok(eye(n))
}
Expr::Zero { rows, cols } => {
let r = dim_usize(rows)?;
let c = dim_usize(cols)?;
check_elems(r, c)?;
Ok(zeros(r, c))
}
Expr::Symbol { name, .. } => Err(format!(
"cannot explicit unbound symbol `{name}`; substitute a dense matrix first"
)),
Expr::Mul { factors } => {
if factors.is_empty() {
return Err("empty MatMul".into());
}
let mut acc = explicit(&factors[0])?;
for f in &factors[1..] {
acc = matmul(&acc, &explicit(f)?)?;
}
Ok(acc)
}
Expr::Add { terms } => {
if terms.is_empty() {
return Err("empty MatAdd".into());
}
let mut acc = explicit(&terms[0])?;
for t in &terms[1..] {
acc = mat_add(&acc, &explicit(t)?)?;
}
Ok(acc)
}
Expr::Sub { left, right } => mat_sub(&explicit(left)?, &explicit(right)?),
Expr::Transpose { arg } => Ok(transpose(&explicit(arg)?)),
Expr::Inv { arg } => invert(&explicit(arg)?),
Expr::Kron { factors } => {
if factors.is_empty() {
return Err("empty Kronecker product".into());
}
let mut acc = explicit(&factors[0])?;
for f in &factors[1..] {
acc = kronecker(&acc, &explicit(f)?)?;
}
Ok(acc)
}
Expr::Block { blocks } => explicit_block(blocks),
}
}
fn explicit_block(blocks: &[Vec<Expr>]) -> Result<Mat, String> {
if blocks.is_empty() || blocks[0].is_empty() {
return Err("empty block matrix".into());
}
let br = blocks.len();
let bc = blocks[0].len();
let mut tiles: Vec<Vec<Mat>> = Vec::with_capacity(br);
for i in 0..br {
let mut row = Vec::with_capacity(bc);
for j in 0..bc {
row.push(explicit(&blocks[i][j])?);
}
tiles.push(row);
}
// Validate and assemble
let mut row_heights = Vec::with_capacity(br);
for i in 0..br {
let (h, _) = shape(&tiles[i][0])?;
for j in 1..bc {
let (h2, _) = shape(&tiles[i][j])?;
if h != h2 {
return Err("block row height mismatch when explicit".into());
}
}
row_heights.push(h);
}
let mut col_widths = Vec::with_capacity(bc);
for j in 0..bc {
let (_, w) = shape(&tiles[0][j])?;
for i in 1..br {
let (_, w2) = shape(&tiles[i][j])?;
if w != w2 {
return Err("block col width mismatch when explicit".into());
}
}
col_widths.push(w);
}
let total_r: usize = row_heights.iter().sum();
let total_c: usize = col_widths.iter().sum();
check_elems(total_r, total_c)?;
let mut out = zeros(total_r, total_c);
let mut r0 = 0;
for i in 0..br {
let mut c0 = 0;
for j in 0..bc {
let tile = &tiles[i][j];
let (h, w) = shape(tile)?;
for ii in 0..h {
for jj in 0..w {
out[r0 + ii][c0 + jj] = tile[ii][jj];
}
}
c0 += w;
}
r0 += row_heights[i];
}
Ok(out)
}
pub fn kronecker(a: &Mat, b: &Mat) -> Result<Mat, String> {
let (ar, ac) = shape(a)?;
let (br, bc) = shape(b)?;
let rows = ar.saturating_mul(br);
let cols = ac.saturating_mul(bc);
check_elems(rows, cols)?;
let mut out = zeros(rows, cols);
for i in 0..ar {
for j in 0..ac {
let aij = a[i][j];
for ii in 0..br {
for jj in 0..bc {
out[i * br + ii][j * bc + jj] = aij * b[ii][jj];
}
}
}
}
Ok(out)
}
pub fn matmul(a: &Mat, b: &Mat) -> Result<Mat, String> {
let (ar, ac) = shape(a)?;
let (br, bc) = shape(b)?;
if ac != br {
return Err(format!(
"matmul shape mismatch: {ar}×{ac} · {br}×{bc}"
));
}
check_elems(ar, bc)?;
let mut out = zeros(ar, bc);
for i in 0..ar {
for k in 0..ac {
let aik = a[i][k];
if aik == 0.0 {
continue;
}
for j in 0..bc {
out[i][j] += aik * b[k][j];
}
}
}
Ok(out)
}
pub fn mat_add(a: &Mat, b: &Mat) -> Result<Mat, String> {
let (ar, ac) = shape(a)?;
let (br, bc) = shape(b)?;
if ar != br || ac != bc {
return Err(format!("add shape mismatch: {ar}×{ac} vs {br}×{bc}"));
}
let mut out = zeros(ar, ac);
for i in 0..ar {
for j in 0..ac {
out[i][j] = a[i][j] + b[i][j];
}
}
Ok(out)
}
pub fn mat_sub(a: &Mat, b: &Mat) -> Result<Mat, String> {
let (ar, ac) = shape(a)?;
let (br, bc) = shape(b)?;
if ar != br || ac != bc {
return Err(format!("sub shape mismatch: {ar}×{ac} vs {br}×{bc}"));
}
let mut out = zeros(ar, ac);
for i in 0..ar {
for j in 0..ac {
out[i][j] = a[i][j] - b[i][j];
}
}
Ok(out)
}
pub fn transpose(m: &Mat) -> Mat {
let (r, c) = shape(m).unwrap_or((0, 0));
let mut out = zeros(c, r);
for i in 0..r {
for j in 0..c {
out[j][i] = m[i][j];
}
}
out
}
pub fn trace(m: &Mat) -> Result<f64, String> {
let (r, c) = shape(m)?;
if r != c {
return Err(format!("trace needs square matrix, got {r}×{c}"));
}
Ok((0..r).map(|i| m[i][i]).sum())
}
pub fn det(m: &Mat) -> Result<f64, String> {
let (n, c) = shape(m)?;
if n != c {
return Err(format!("det needs square matrix, got {n}×{c}"));
}
if n == 0 {
return Ok(1.0);
}
if n == 1 {
return Ok(m[0][0]);
}
if n == 2 {
return Ok(m[0][0] * m[1][1] - m[0][1] * m[1][0]);
}
// Gaussian elimination with partial pivoting
let mut a = m.clone();
let mut det = 1.0;
for k in 0..n {
let mut piv = k;
let mut best = a[k][k].abs();
for i in (k + 1)..n {
let v = a[i][k].abs();
if v > best {
best = v;
piv = i;
}
}
if best < 1e-15 {
return Ok(0.0);
}
if piv != k {
a.swap(k, piv);
det = -det;
}
let akk = a[k][k];
det *= akk;
for i in (k + 1)..n {
let f = a[i][k] / akk;
for j in k..n {
a[i][j] -= f * a[k][j];
}
}
}
Ok(det)
}
pub fn invert(m: &Mat) -> Result<Mat, String> {
let (n, c) = shape(m)?;
if n != c {
return Err(format!("inverse needs square matrix, got {n}×{c}"));
}
check_elems(n, n)?;
// Augment [A|I]
let mut a = zeros(n, 2 * n);
for i in 0..n {
for j in 0..n {
a[i][j] = m[i][j];
}
a[i][n + i] = 1.0;
}
for k in 0..n {
let mut piv = k;
let mut best = a[k][k].abs();
for i in (k + 1)..n {
let v = a[i][k].abs();
if v > best {
best = v;
piv = i;
}
}
if best < 1e-15 {
return Err("matrix is singular (not invertible)".into());
}
if piv != k {
a.swap(k, piv);
}
let akk = a[k][k];
for j in 0..(2 * n) {
a[k][j] /= akk;
}
for i in 0..n {
if i == k {
continue;
}
let f = a[i][k];
for j in 0..(2 * n) {
a[i][j] -= f * a[k][j];
}
}
}
let mut out = zeros(n, n);
for i in 0..n {
for j in 0..n {
out[i][j] = a[i][n + j];
}
}
Ok(out)
}
/// Solve A x = b. `b` may be n×1 or n×k; returns n×k.
pub fn solve(a: &Mat, b: &Mat) -> Result<Mat, String> {
let (n, ac) = shape(a)?;
if n != ac {
return Err(format!("solve needs square A, got {n}×{ac}"));
}
let (br, bc) = shape(b)?;
if br != n {
return Err(format!("solve rhs rows {br} != A rows {n}"));
}
// Augment [A|B]
let mut m = zeros(n, n + bc);
for i in 0..n {
for j in 0..n {
m[i][j] = a[i][j];
}
for j in 0..bc {
m[i][n + j] = b[i][j];
}
}
for k in 0..n {
let mut piv = k;
let mut best = m[k][k].abs();
for i in (k + 1)..n {
let v = m[i][k].abs();
if v > best {
best = v;
piv = i;
}
}
if best < 1e-15 {
return Err("singular matrix in solve".into());
}
if piv != k {
m.swap(k, piv);
}
let akk = m[k][k];
for j in k..(n + bc) {
m[k][j] /= akk;
}
for i in 0..n {
if i == k {
continue;
}
let f = m[i][k];
for j in k..(n + bc) {
m[i][j] -= f * m[k][j];
}
}
}
let mut x = zeros(n, bc);
for i in 0..n {
for j in 0..bc {
x[i][j] = m[i][n + j];
}
}
Ok(x)
}
pub fn solve_result(a: &Mat, b: &Mat) -> Result<Value, String> {
let x = solve(a, b)?;
let ax = matmul(a, &x)?;
let resid = mat_sub(&ax, b)?;
let mut max_r = 0.0_f64;
for row in &resid {
for &v in row {
max_r = max_r.max(v.abs());
}
}
Ok(json!({
"_type": "linalg_solve",
"method": "gauss",
"x": to_dense_value(&x)?,
"residual_max": max_r,
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn solve_2x2() {
let a = vec![vec![2.0, 1.0], vec![1.0, 3.0]];
let b = vec![vec![5.0], vec![5.0]];
let x = solve(&a, &b).unwrap();
assert!((x[0][0] - 2.0).abs() < 1e-9);
assert!((x[1][0] - 1.0).abs() < 1e-9);
}
#[test]
fn det_2x2() {
let m = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
assert!((det(&m).unwrap() + 2.0).abs() < 1e-9);
}
#[test]
fn explicit_mul() {
let a = Expr::Dense {
data: vec![vec![1.0, 2.0], vec![0.0, 1.0]],
};
let b = Expr::Dense {
data: vec![vec![1.0, 0.0], vec![1.0, 1.0]],
};
let p = Expr::Mul {
factors: vec![a, b],
};
let m = explicit(&p).unwrap();
assert_eq!(m, vec![vec![3.0, 2.0], vec![1.0, 1.0]]);
}
#[test]
fn oversize_guard() {
assert!(check_elems(101, 100).is_err());
}
}
+361
View File
@@ -0,0 +1,361 @@
//! Structure SVG for factorizations (teaching layouts) with Q8-aligned themes.
use serde_json::Value;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThemeName {
Dark,
Light,
Bw,
}
impl ThemeName {
pub fn parse(raw: &str) -> Result<Self, String> {
match raw.trim().to_ascii_lowercase().as_str() {
"dark" | "暗" => Ok(Self::Dark),
"light" | "亮" | "浅" => Ok(Self::Light),
"bw" | "mono" | "黑白" => Ok(Self::Bw),
other => Err(format!("unknown theme `{other}` (dark|light|bw)")),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Dark => "dark",
Self::Light => "light",
Self::Bw => "bw",
}
}
}
struct Theme {
name: ThemeName,
bg: &'static str,
ink: &'static str,
muted: &'static str,
stroke: &'static str,
box_a: &'static str,
box_b: &'static str,
box_c: &'static str,
heat_neg: &'static str,
heat_pos: &'static str,
}
impl Theme {
fn of(name: ThemeName) -> Self {
match name {
ThemeName::Dark => Self {
name,
bg: "#0a0e14",
ink: "#e8eef6",
muted: "#93a4b8",
stroke: "#6b7f96",
box_a: "#1e3a5f",
box_b: "#3d2e14",
box_c: "#1e3f2e",
heat_neg: "#cc5555",
heat_pos: "#4c8fff",
},
ThemeName::Light => Self {
name,
bg: "#eef3f9",
ink: "#15202b",
muted: "#5a6a7a",
stroke: "#4a5a6a",
box_a: "#e8f1ff",
box_b: "#fff3d6",
box_c: "#e8ffe8",
heat_neg: "#cc3333",
heat_pos: "#2266aa",
},
ThemeName::Bw => Self {
name,
bg: "#ffffff",
ink: "#111111",
muted: "#555555",
stroke: "#222222",
box_a: "#f0f0f0",
box_b: "#e0e0e0",
box_c: "#d0d0d0",
heat_neg: "#000000",
heat_pos: "#666666",
},
}
}
}
/// Draw a factor object or matrix as structure SVG.
pub fn draw_factor(
factor: &Value,
kind: Option<&str>,
path: Option<&str>,
theme: ThemeName,
) -> Result<String, String> {
let kind = kind
.or_else(|| factor.get("kind").and_then(|k| k.as_str()))
.unwrap_or("structure");
let th = Theme::of(theme);
let svg = match kind {
"eig" => draw_eig(factor, &th),
"svd" => draw_svd(factor, &th),
"qr" => draw_qr(factor, &th),
"lu" | "ge" => draw_lu(factor, &th),
"chol" => draw_chol(factor, &th),
"structure" => draw_structure(factor, &th),
"heatmap" | "hinton" => draw_heatmap(factor, kind == "hinton", &th),
other => {
return Err(format!(
"unknown draw kind `{other}` (want eig|svd|qr|lu|ge|chol|structure|heatmap|hinton)"
))
}
};
let _ = path;
Ok(svg)
}
fn escape(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
}
fn box_svg(x: f64, y: f64, w: f64, h: f64, label: &str, fill: &str, th: &Theme) -> String {
format!(
"<rect x=\"{x}\" y=\"{y}\" width=\"{w}\" height=\"{h}\" fill=\"{fill}\" stroke=\"{}\" stroke-width=\"1.5\" rx=\"4\"/>\
<text x=\"{}\" y=\"{}\" text-anchor=\"middle\" dominant-baseline=\"middle\" font-size=\"16\" font-family=\"serif\" fill=\"{}\">{}</text>",
th.stroke,
x + w / 2.0,
y + h / 2.0,
th.ink,
escape(label)
)
}
fn root(w: f64, h: f64, kind: &str, th: &Theme, body: &str) -> String {
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" data-linalg=\"{kind}\" data-theme=\"{}\" style=\"background:{}\">\
{body}\
</svg>",
th.name.as_str(),
th.bg
)
}
fn draw_eig(factor: &Value, th: &Theme) -> String {
let latex = factor
.get("latex")
.and_then(|x| x.as_str())
.unwrap_or("A = P D P^{-1}");
let evals = factor
.get("eigenvalues")
.and_then(|x| x.as_array())
.map(|a| {
a.iter()
.filter_map(|v| v.as_f64())
.map(|v| format!("{v:.4}"))
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">{}</text>\
{}\
{}\
{}\
<text x=\"16\" y=\"120\" font-size=\"13\" font-family=\"sans-serif\" fill=\"{}\">λ = [{}]</text>",
th.ink,
escape(latex),
box_svg(40.0, 48.0, 70.0, 50.0, "P", th.box_a, th),
box_svg(130.0, 48.0, 70.0, 50.0, "D", th.box_b, th),
box_svg(220.0, 48.0, 90.0, 50.0, "P⁻¹", th.box_a, th),
th.muted,
escape(&evals)
);
root(520.0, 140.0, "eig", th, &body)
}
fn draw_svd(factor: &Value, th: &Theme) -> String {
let s = factor
.get("S")
.and_then(|x| x.as_array())
.map(|a| {
a.iter()
.filter_map(|v| v.as_f64())
.map(|v| format!("{v:.4}"))
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = U Σ V⊤</text>\
{}\
{}\
{}\
<text x=\"16\" y=\"120\" font-size=\"13\" font-family=\"sans-serif\" fill=\"{}\">σ = [{}]</text>",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "U", th.box_a, th),
box_svg(130.0, 48.0, 70.0, 50.0, "Σ", th.box_b, th),
box_svg(220.0, 48.0, 80.0, 50.0, "V⊤", th.box_a, th),
th.muted,
escape(&s)
);
root(560.0, 140.0, "svd", th, &body)
}
fn draw_qr(_factor: &Value, th: &Theme) -> String {
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = Q R</text>\
{}\
{}",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "Q", th.box_a, th),
box_svg(130.0, 48.0, 70.0, 50.0, "R", th.box_b, th),
);
root(420.0, 120.0, "qr", th, &body)
}
fn draw_lu(_factor: &Value, th: &Theme) -> String {
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = L U</text>\
{}\
{}",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "L", th.box_c, th),
box_svg(130.0, 48.0, 70.0, 50.0, "U", th.box_b, th),
);
root(420.0, 120.0, "lu", th, &body)
}
fn draw_chol(_factor: &Value, th: &Theme) -> String {
let body = format!(
"<text x=\"16\" y=\"28\" font-size=\"18\" font-family=\"serif\" fill=\"{}\">A = L L⊤</text>\
{}\
{}",
th.ink,
box_svg(40.0, 48.0, 70.0, 50.0, "L", th.box_c, th),
box_svg(130.0, 48.0, 80.0, 50.0, "L⊤", th.box_c, th),
);
root(420.0, 120.0, "chol", th, &body)
}
fn draw_structure(factor: &Value, th: &Theme) -> String {
let label = if let (Some(r), Some(c)) = (factor.get("rows"), factor.get("cols")) {
format!("{}×{}", r, c)
} else if let Some(Value::Array(a)) = factor.get("shape") {
if a.len() >= 2 {
format!("{}×{}", a[0], a[1])
} else {
factor
.get("kind")
.and_then(|k| k.as_str())
.unwrap_or("matrix")
.to_string()
}
} else {
factor
.get("kind")
.and_then(|k| k.as_str())
.unwrap_or("matrix")
.to_string()
};
let body = box_svg(40.0, 30.0, 160.0, 60.0, &label, th.box_a, th);
root(280.0, 120.0, "structure", th, &body)
}
fn draw_heatmap(factor: &Value, hinton: bool, th: &Theme) -> String {
let data = factor
.get("data")
.or_else(|| factor.get("eigenvectors").and_then(|v| v.get("data")))
.and_then(|d| d.as_array());
let Some(rows) = data else {
return draw_structure(factor, th);
};
let n = rows.len().min(16);
let m = rows
.first()
.and_then(|r| r.as_array())
.map(|r| r.len())
.unwrap_or(0)
.min(16);
if n == 0 || m == 0 {
return draw_structure(factor, th);
}
let mut max_abs: f64 = 1e-12;
for row in rows.iter().take(n) {
if let Some(cells) = row.as_array() {
for c in cells.iter().take(m) {
let v = cell_mag(c);
max_abs = max_abs.max(v);
}
}
}
let cell = 18.0;
let w = 40.0 + m as f64 * cell;
let h = 40.0 + n as f64 * cell;
let tag = if hinton { "hinton" } else { "heatmap" };
let mut parts = vec![format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" data-linalg=\"{tag}\" data-theme=\"{}\" style=\"background:{}\">",
th.name.as_str(),
th.bg
)];
for (i, row) in rows.iter().take(n).enumerate() {
let Some(cells) = row.as_array() else { continue };
for (j, c) in cells.iter().take(m).enumerate() {
let v = cell_signed(c);
let x = 20.0 + j as f64 * cell;
let y = 20.0 + i as f64 * cell;
if hinton {
let s = (v.abs() / max_abs).sqrt() * (cell - 4.0);
let fill = if v >= 0.0 { th.heat_pos } else { th.heat_neg };
parts.push(format!(
"<rect x=\"{}\" y=\"{}\" width=\"{s}\" height=\"{s}\" fill=\"{fill}\"/>",
x + (cell - s) / 2.0,
y + (cell - s) / 2.0
));
} else {
let t = ((v / max_abs) + 1.0) * 0.5;
let r = (255.0 * (1.0 - t)) as u8;
let b = (255.0 * t) as u8;
let fill = if th.name == ThemeName::Bw {
let g = (255.0 * (1.0 - v.abs() / max_abs)) as u8;
format!("rgb({g},{g},{g})")
} else {
format!("rgb({r},80,{b})")
};
parts.push(format!(
"<rect x=\"{x}\" y=\"{y}\" width=\"{cell}\" height=\"{cell}\" fill=\"{fill}\" stroke=\"{}\" stroke-width=\"0.5\"/>",
th.bg
));
}
}
}
parts.push("</svg>".into());
parts.join("")
}
fn cell_mag(c: &Value) -> f64 {
if let Some(v) = c.as_f64() {
return v.abs();
}
if let Some(a) = c.as_array() {
if a.len() >= 2 {
let re = a[0].as_f64().unwrap_or(0.0);
let im = a[1].as_f64().unwrap_or(0.0);
return (re * re + im * im).sqrt();
}
}
0.0
}
fn cell_signed(c: &Value) -> f64 {
if let Some(v) = c.as_f64() {
return v;
}
// complex: use real part for signed heatmap, magnitude for abs
if let Some(a) = c.as_array() {
if a.len() >= 2 {
return a[0].as_f64().unwrap_or(0.0);
}
}
0.0
}
+451
View File
@@ -0,0 +1,451 @@
//! Dense factorizations for L4: LU, QR, eig (symmetric Jacobi), SVD, Cholesky.
use serde_json::{json, Value};
use crate::dense::{self, Mat, MAX_ELEMENTS};
pub const FACTOR_MAX_SIDE: usize = 64;
fn check_side(n: usize, label: &str) -> Result<(), String> {
if n > FACTOR_MAX_SIDE {
return Err(format!(
"{label} limited to side ≤ {FACTOR_MAX_SIDE} (got {n})"
));
}
Ok(())
}
pub fn check_side_limit(n: usize, label: &str) -> Result<(), String> {
check_side(n, label)
}
fn check_mat(m: &Mat) -> Result<(usize, usize), String> {
let (r, c) = dense::shape(m)?;
dense::check_elems(r, c)?;
Ok((r, c))
}
/// Factorize `kind` ∈ lu|qr|svd|eig|chol.
pub fn factorize(m: &Mat, kind: &str) -> Result<Value, String> {
let kind = kind.trim().to_ascii_lowercase();
match kind.as_str() {
"lu" => lu_value(m),
"qr" => qr_value(m),
"eig" => eig_value(m),
"svd" => svd_value(m),
"chol" | "cholesky" => chol_value(m),
other => Err(format!(
"unknown factorize kind `{other}` (want lu|qr|svd|eig|chol)"
)),
}
}
fn lu_value(m: &Mat) -> Result<Value, String> {
let (n, c) = check_mat(m)?;
if n != c {
return Err(format!("LU needs square matrix, got {n}×{c}"));
}
check_side(n, "LU")?;
let (l, u, piv) = lu_decomp(m)?;
Ok(json!({
"_type": "linalg_factor",
"kind": "lu",
"L": dense::to_dense_value(&l)?,
"U": dense::to_dense_value(&u)?,
"pivots": piv,
"latex": "A = LU",
"ascii": "LU",
}))
}
fn qr_value(m: &Mat) -> Result<Value, String> {
let (rows, cols) = check_mat(m)?;
check_side(rows.max(cols), "QR")?;
let (q, r) = qr_decomp(m)?;
Ok(json!({
"_type": "linalg_factor",
"kind": "qr",
"Q": dense::to_dense_value(&q)?,
"R": dense::to_dense_value(&r)?,
"latex": "A = QR",
"ascii": "QR",
}))
}
fn eig_value(m: &Mat) -> Result<Value, String> {
let (n, c) = check_mat(m)?;
if n != c {
return Err(format!("eig needs square matrix, got {n}×{c}"));
}
check_side(n, "eig")?;
// Jacobi assumes symmetric; if nearly symmetric, symmetrize.
let (evals, evecs) = jacobi_symm(m)?;
let d = dense::zeros(n, n);
let mut d = d;
for i in 0..n {
d[i][i] = evals[i];
}
Ok(json!({
"_type": "linalg_factor",
"kind": "eig",
"eigenvalues": evals,
"eigenvectors": dense::to_dense_value(&evecs)?,
"D": dense::to_dense_value(&d)?,
"latex": "A = P D P^{-1}",
"ascii": "eig",
}))
}
fn svd_value(m: &Mat) -> Result<Value, String> {
let (rows, cols) = check_mat(m)?;
check_side(rows.max(cols), "SVD")?;
let (u, s, vt) = svd_decomp(m)?;
Ok(json!({
"_type": "linalg_factor",
"kind": "svd",
"U": dense::to_dense_value(&u)?,
"S": s,
"Vt": dense::to_dense_value(&vt)?,
"latex": "A = U \\Sigma V^{\\top}",
"ascii": "SVD",
}))
}
fn chol_value(m: &Mat) -> Result<Value, String> {
let (n, c) = check_mat(m)?;
if n != c {
return Err(format!("Cholesky needs square matrix, got {n}×{c}"));
}
check_side(n, "Cholesky")?;
let l = cholesky(m)?;
Ok(json!({
"_type": "linalg_factor",
"kind": "chol",
"L": dense::to_dense_value(&l)?,
"latex": "A = L L^{\\top}",
"ascii": "chol",
}))
}
/// PA = LU with partial pivoting; returns (L, U, pivot row order).
pub fn lu_decomp(a: &Mat) -> Result<(Mat, Mat, Vec<usize>), String> {
let (n, _) = dense::shape(a)?;
let mut m = a.clone();
let mut piv: Vec<usize> = (0..n).collect();
let mut l = dense::eye(n);
for k in 0..n {
let mut piv_row = k;
let mut best = m[k][k].abs();
for i in (k + 1)..n {
let v = m[i][k].abs();
if v > best {
best = v;
piv_row = i;
}
}
if best < 1e-15 {
return Err("LU failed: singular or near-singular matrix".into());
}
if piv_row != k {
m.swap(k, piv_row);
piv.swap(k, piv_row);
for j in 0..k {
let t = l[k][j];
l[k][j] = l[piv_row][j];
l[piv_row][j] = t;
}
}
for i in (k + 1)..n {
let f = m[i][k] / m[k][k];
l[i][k] = f;
m[i][k] = 0.0;
for j in (k + 1)..n {
m[i][j] -= f * m[k][j];
}
}
}
Ok((l, m, piv))
}
/// Thin QR via modified Gram-Schmidt. A is m×n with m≥n preferred.
pub fn qr_decomp(a: &Mat) -> Result<(Mat, Mat), String> {
let (m, n) = dense::shape(a)?;
let mut v = a.clone();
let mut q = dense::zeros(m, n);
let mut r = dense::zeros(n, n);
for j in 0..n {
// orthogonalize against previous
for i in 0..j {
let mut dot = 0.0;
for k in 0..m {
dot += q[k][i] * v[k][j];
}
r[i][j] = dot;
for k in 0..m {
v[k][j] -= dot * q[k][i];
}
}
let mut norm = 0.0;
for k in 0..m {
norm += v[k][j] * v[k][j];
}
norm = norm.sqrt();
if norm < 1e-15 {
return Err("QR failed: linearly dependent columns".into());
}
r[j][j] = norm;
for k in 0..m {
q[k][j] = v[k][j] / norm;
}
}
Ok((q, r))
}
/// Jacobi eigenvalue for symmetric (or symmetrized) real matrix.
/// Returns eigenvalues (descending) and eigenvector matrix (columns).
pub fn jacobi_symm(a: &Mat) -> Result<(Vec<f64>, Mat), String> {
let (n, _) = dense::shape(a)?;
// Symmetrize
let mut s = dense::zeros(n, n);
for i in 0..n {
for j in 0..n {
s[i][j] = 0.5 * (a[i][j] + a[j][i]);
}
}
let mut v = dense::eye(n);
for _ in 0..(n * n * 40).max(40) {
// Find largest off-diagonal
let mut p = 0usize;
let mut q = 1usize;
let mut best = 0.0;
for i in 0..n {
for j in (i + 1)..n {
let v = s[i][j].abs();
if v > best {
best = v;
p = i;
q = j;
}
}
}
if best < 1e-12 {
break;
}
let app = s[p][p];
let aqq = s[q][q];
let apq = s[p][q];
let tau = (aqq - app) / (2.0 * apq);
let t = if tau >= 0.0 {
1.0 / (tau + (1.0 + tau * tau).sqrt())
} else {
-1.0 / (-tau + (1.0 + tau * tau).sqrt())
};
let c = 1.0 / (1.0 + t * t).sqrt();
let sn = t * c;
// Rotate S
s[p][p] = app - t * apq;
s[q][q] = aqq + t * apq;
s[p][q] = 0.0;
s[q][p] = 0.0;
for i in 0..n {
if i == p || i == q {
continue;
}
let sip = s[i][p];
let siq = s[i][q];
s[i][p] = c * sip - sn * siq;
s[p][i] = s[i][p];
s[i][q] = c * siq + sn * sip;
s[q][i] = s[i][q];
}
// Accumulate V
for i in 0..n {
let vip = v[i][p];
let viq = v[i][q];
v[i][p] = c * vip - sn * viq;
v[i][q] = c * viq + sn * vip;
}
}
let mut pairs: Vec<(f64, usize)> = (0..n).map(|i| (s[i][i], i)).collect();
pairs.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
let mut evals = Vec::with_capacity(n);
let mut evecs = dense::zeros(n, n);
for (col, &(ev, idx)) in pairs.iter().enumerate() {
evals.push(ev);
for row in 0..n {
evecs[row][col] = v[row][idx];
}
}
Ok((evals, evecs))
}
/// Economy SVD via eig of AᵀA (n small).
pub fn svd_decomp(a: &Mat) -> Result<(Mat, Vec<f64>, Mat), String> {
let (m, n) = dense::shape(a)?;
// Gram = Aᵀ A (n×n)
let at = dense::transpose(a);
let gram = dense::matmul(&at, a)?;
let (evals, v) = jacobi_symm(&gram)?;
let mut svals = Vec::with_capacity(n.min(m));
let rank = n.min(m);
for i in 0..rank {
let s = if evals[i] > 0.0 { evals[i].sqrt() } else { 0.0 };
svals.push(s);
}
// Vt = Vᵀ
let vt = dense::transpose(&v);
// U = A V S^{+}
let mut u = dense::zeros(m, rank);
for j in 0..rank {
if svals[j] < 1e-15 {
continue;
}
// u_j = A v_j / s_j
for i in 0..m {
let mut sum = 0.0;
for k in 0..n {
sum += a[i][k] * v[k][j];
}
u[i][j] = sum / svals[j];
}
}
// Truncate Vt to rank rows
let mut vt_r = dense::zeros(rank, n);
for i in 0..rank {
for j in 0..n {
vt_r[i][j] = vt[i][j];
}
}
let _ = MAX_ELEMENTS;
Ok((u, svals, vt_r))
}
pub fn cholesky(a: &Mat) -> Result<Mat, String> {
let (n, _) = dense::shape(a)?;
let mut l = dense::zeros(n, n);
for i in 0..n {
for j in 0..=i {
let mut sum = 0.0;
for k in 0..j {
sum += l[i][k] * l[j][k];
}
if i == j {
let v = a[i][i] - sum;
if v <= 1e-15 {
return Err("Cholesky failed: matrix not positive definite".into());
}
l[i][j] = v.sqrt();
} else {
if l[j][j].abs() < 1e-15 {
return Err("Cholesky failed: zero pivot".into());
}
l[i][j] = (a[i][j] - sum) / l[j][j];
}
}
}
Ok(l)
}
/// Forward/back-sub with LU from `factorize kind=lu` (pivots applied to rhs).
pub fn solve_lu_factor(factor: &Value, b: &Mat) -> Result<Mat, String> {
if factor.get("kind").and_then(|k| k.as_str()) != Some("lu") {
return Err("solve(factor=…) needs kind=lu factor".into());
}
let l = dense::from_value(factor.get("L").ok_or_else(|| "factor missing L".to_string())?)?;
let u = dense::from_value(factor.get("U").ok_or_else(|| "factor missing U".to_string())?)?;
let piv = factor
.get("pivots")
.and_then(|p| p.as_array())
.ok_or_else(|| "factor missing pivots".to_string())?;
let (n, _) = dense::shape(&l)?;
let (br, bc) = dense::shape(b)?;
if br != n {
return Err(format!("rhs rows {br} != factor size {n}"));
}
let mut pb = dense::zeros(n, bc);
for (i, p) in piv.iter().enumerate() {
let src = p
.as_u64()
.or_else(|| p.as_i64().map(|x| x as u64))
.ok_or_else(|| "bad pivot".to_string())? as usize;
if src >= n {
return Err("pivot out of range".into());
}
for j in 0..bc {
pb[i][j] = b[src][j];
}
}
// Ly = Pb
let mut y = dense::zeros(n, bc);
for i in 0..n {
for j in 0..bc {
let mut s = pb[i][j];
for k in 0..i {
s -= l[i][k] * y[k][j];
}
y[i][j] = s / l[i][i];
}
}
// Ux = y
let mut x = dense::zeros(n, bc);
for i in (0..n).rev() {
for j in 0..bc {
let mut s = y[i][j];
for k in (i + 1)..n {
s -= u[i][k] * x[k][j];
}
if u[i][i].abs() < 1e-15 {
return Err("singular U in LU solve".into());
}
x[i][j] = s / u[i][i];
}
}
Ok(x)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn eig_diag() {
let m = vec![vec![2.0, 0.0], vec![0.0, 3.0]];
let (ev, _) = jacobi_symm(&m).unwrap();
assert!((ev[0] - 3.0).abs() < 1e-9);
assert!((ev[1] - 2.0).abs() < 1e-9);
}
#[test]
fn qr_roundtrip() {
let a = vec![vec![1.0, 1.0], vec![0.0, 1.0], vec![1.0, 0.0]];
let (q, r) = qr_decomp(&a).unwrap();
let recon = dense::matmul(&q, &r).unwrap();
for i in 0..3 {
for j in 0..2 {
assert!((recon[i][j] - a[i][j]).abs() < 1e-9);
}
}
}
#[test]
fn lu_roundtrip_no_pivot_needed() {
let a = vec![vec![2.0, 1.0], vec![1.0, 3.0]];
let (l, u, _) = lu_decomp(&a).unwrap();
let recon = dense::matmul(&l, &u).unwrap();
for i in 0..2 {
for j in 0..2 {
assert!((recon[i][j] - a[i][j]).abs() < 1e-9);
}
}
}
#[test]
fn lu_solve_reuse() {
let a = vec![vec![2.0, 1.0], vec![1.0, 3.0]];
let b = vec![vec![5.0], vec![5.0]];
let f = factorize(&a, "lu").unwrap();
let x = solve_lu_factor(&f, &b).unwrap();
assert!((x[0][0] - 2.0).abs() < 1e-9);
assert!((x[1][0] - 1.0).abs() < 1e-9);
}
}
+688
View File
@@ -0,0 +1,688 @@
//! Marqdo linalg plugin (C ABI v2): formula-first matrix expressions.
mod complex;
mod dense;
mod draw;
mod factor;
mod matexpr;
mod metrics;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr;
use serde_json::{json, Value};
use matexpr::{Dim, Expr};
const ABI_VERSION: u32 = 2;
type PluginFn = unsafe extern "C" fn(
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int;
type HostQueryFn = unsafe extern "C" fn(
userdata: *mut c_void,
name: *const c_char,
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int;
#[repr(C)]
pub struct MarqdoHostApi {
pub userdata: *mut c_void,
pub register_fn: Option<
unsafe extern "C" fn(
userdata: *mut c_void,
name: *const c_char,
params: *const c_char,
fn_ptr: PluginFn,
) -> c_int,
>,
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
pub host_query: Option<HostQueryFn>,
}
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
static mut HOST_QUERY: Option<HostQueryFn> = None;
static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
unsafe fn host_strdup(s: &str) -> *mut c_char {
let alloc = HOST_ALLOC.expect("host alloc");
let bytes = s.as_bytes();
let p = alloc(bytes.len() + 1) as *mut u8;
if p.is_null() {
return ptr::null_mut();
}
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
*p.add(bytes.len()) = 0;
p as *mut c_char
}
fn set_out(out: *mut *mut c_char, s: &str) {
if out.is_null() {
return;
}
unsafe {
*out = host_strdup(s);
}
}
fn set_err(err: *mut *mut c_char, s: &str) {
if err.is_null() {
return;
}
unsafe {
*err = host_strdup(s);
}
}
fn parse_args(args_json: *const c_char) -> Result<Value, String> {
if args_json.is_null() {
return Ok(json!({}));
}
let s = unsafe { CStr::from_ptr(args_json) }
.to_str()
.map_err(|_| "args not utf-8".to_string())?;
if s.trim().is_empty() {
return Ok(json!({}));
}
serde_json::from_str(s).map_err(|e| e.to_string())
}
fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
match r {
Ok(v) => {
set_out(out_json, &v.to_string());
0
}
Err(e) => {
set_err(err_msg, &e);
1
}
}
}
macro_rules! la_ffi {
($name:ident, $body:expr) => {
unsafe extern "C" fn $name(
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int {
let args = match parse_args(args_json) {
Ok(v) => v,
Err(e) => {
set_err(err_msg, &format!("{}: {e}", stringify!($name)));
return 1;
}
};
let r: Result<Value, String> = (|| $body(&args))();
reply(
out_json,
err_msg,
r.map_err(|e| format!("{}: {e}", stringify!($name))),
)
}
};
}
fn take_host_string(p: *mut c_char) -> Option<String> {
if p.is_null() {
return None;
}
let s = unsafe { CStr::from_ptr(p) }
.to_str()
.ok()
.map(|s| s.to_string());
unsafe {
if let Some(free) = HOST_FREE {
free(p as *mut c_void);
}
}
s
}
fn host_query_json(name: &str, args: &Value) -> Result<Value, String> {
let query = unsafe { HOST_QUERY }.ok_or_else(|| "host_query not available".to_string())?;
let userdata = unsafe { HOST_USERDATA };
let c_name = CString::new(name).map_err(|e| e.to_string())?;
let c_args = CString::new(args.to_string()).map_err(|e| e.to_string())?;
let mut out_ptr: *mut c_char = ptr::null_mut();
let mut err_ptr: *mut c_char = ptr::null_mut();
let rc = unsafe {
query(
userdata,
c_name.as_ptr(),
c_args.as_ptr(),
&mut out_ptr,
&mut err_ptr,
)
};
let err = take_host_string(err_ptr);
let out = take_host_string(out_ptr);
if rc != 0 {
return Err(err.unwrap_or_else(|| format!("host_query `{name}` failed")));
}
let out = out.unwrap_or_else(|| "null".into());
serde_json::from_str(&out).map_err(|e| format!("host_query `{name}` bad JSON: {e}"))
}
fn record_plot(svg: &str, path: Option<&str>) -> Result<(), String> {
let mut args = json!({ "svg": svg });
if let Some(p) = path {
if !p.is_empty() {
args.as_object_mut()
.unwrap()
.insert("path".into(), json!(p));
}
}
host_query_json("record_plot", &args)?;
Ok(())
}
fn arg_dim(v: &Value, key: &str) -> Result<Dim, String> {
Dim::from_json(v.get(key).ok_or_else(|| format!("missing `{key}`"))?)
}
fn arg_expr_opt(v: &Value, keys: &[&str]) -> Result<Expr, String> {
for k in keys {
if let Some(e) = v.get(*k) {
if !e.is_null() {
return coerce_expr(e);
}
}
}
Err(format!("missing one of {:?}", keys))
}
fn coerce_expr(v: &Value) -> Result<Expr, String> {
if let Ok(e) = Expr::from_value(v) {
return Ok(e);
}
let data = dense::from_value(v)?;
Ok(Expr::Dense { data })
}
fn wrap(expr: Expr) -> Result<Value, String> {
expr.to_value()
}
la_ffi!(linalg_ping, |_args: &Value| {
Ok(json!({
"ok": true,
"name": "linalg",
"abi": ABI_VERSION,
"features": [
"matexpr",
"simplify",
"dense",
"solve",
"block",
"kron",
"factorize",
"draw",
"lstsq",
"norm",
"cond",
"rank",
"complex",
"declare",
],
}))
});
la_ffi!(linalg_symbol, |args: &Value| {
let name = args
.get("name")
.or_else(|| args.get("名"))
.and_then(|x| x.as_str())
.ok_or_else(|| "missing `name`".to_string())?;
let rows = arg_dim(args, "rows").or_else(|_| arg_dim(args, "行"))?;
let cols = arg_dim(args, "cols").or_else(|_| arg_dim(args, "列"))?;
wrap(matexpr::symbol(name, rows, cols))
});
la_ffi!(linalg_eye, |args: &Value| {
let n = arg_dim(args, "n")
.or_else(|_| arg_dim(args, "阶"))
.or_else(|_| arg_dim(args, "rows"))?;
wrap(matexpr::eye(n))
});
la_ffi!(linalg_zeros, |args: &Value| {
let rows = arg_dim(args, "rows").or_else(|_| arg_dim(args, "行"))?;
let cols = arg_dim(args, "cols").or_else(|_| arg_dim(args, "列"))?;
wrap(matexpr::zeros(rows, cols))
});
la_ffi!(linalg_from_list, |args: &Value| {
let data = args
.get("data")
.or_else(|| args.get("数据"))
.ok_or_else(|| "missing `data`".to_string())?;
if complex::data_has_complex(data) {
return complex::to_dense_value(&complex::from_data(data)?);
}
let m = dense::from_value(data)?;
wrap(matexpr::from_list(m)?)
});
la_ffi!(linalg_declare, |args: &Value| {
let table = args
.get("table")
.or_else(|| args.get("rows"))
.or_else(|| args.get("表"))
.or_else(|| args.get("行"))
.ok_or_else(|| "missing `table` (list of {name,rows,cols})".to_string())?;
let rows = table
.as_array()
.ok_or_else(|| "`table` must be a list of row maps".to_string())?;
let tag = args
.get("type_tag")
.or_else(|| args.get("类型标签"))
.and_then(|x| x.as_str())
.unwrap_or("matrix");
let mut out = serde_json::Map::new();
out.insert("_type".into(), json!("matrix_env"));
for (i, row) in rows.iter().enumerate() {
let obj = row
.as_object()
.ok_or_else(|| format!("declare row {i} must be a map"))?;
let name = obj
.get("name")
.or_else(|| obj.get("名"))
.and_then(|x| x.as_str())
.ok_or_else(|| format!("declare row {i} missing name"))?
.to_string();
let r = Dim::from_json(
obj.get("rows")
.or_else(|| obj.get("行"))
.ok_or_else(|| format!("declare `{name}` missing rows"))?,
)?;
let c = Dim::from_json(
obj.get("cols")
.or_else(|| obj.get("列"))
.ok_or_else(|| format!("declare `{name}` missing cols"))?,
)?;
let expr = matexpr::symbol(&name, r, c);
let mut v = expr.to_value()?;
if let Some(m) = v.as_object_mut() {
m.insert("_type".into(), json!(tag));
}
out.insert(name, v);
}
Ok(Value::Object(out))
});
la_ffi!(linalg_mul, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::mul(a, b)?)
});
la_ffi!(linalg_add, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::add(a, b)?)
});
la_ffi!(linalg_sub, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::sub(a, b)?)
});
la_ffi!(linalg_transpose, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::transpose(e)?)
});
la_ffi!(linalg_inv, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::inv(e)?)
});
la_ffi!(linalg_simplify, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::simplify(e)?)
});
fn display_expr(args: &Value, e: matexpr::Expr) -> Result<matexpr::Expr, String> {
let raw = args
.get("raw")
.or_else(|| args.get("原始"))
.and_then(|v| match v {
Value::Bool(b) => Some(*b),
Value::String(s) => Some(matches!(s.to_ascii_lowercase().as_str(), "true" | "1" | "yes")),
_ => v.as_bool(),
})
.unwrap_or(false);
if raw {
Ok(e)
} else {
matexpr::simplify(e)
}
}
la_ffi!(linalg_ascii, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
let e = display_expr(args, e)?;
Ok(json!(e.ascii()))
});
la_ffi!(linalg_latex, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
let e = display_expr(args, e)?;
Ok(json!(e.latex()))
});
la_ffi!(linalg_shape, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
let (r, c) = e.shape()?;
Ok(json!([r.to_json(), c.to_json()]))
});
la_ffi!(linalg_show, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
let e = display_expr(args, e)?;
let latex = e.latex();
let ascii = e.ascii();
let path = args
.get("path")
.or_else(|| args.get("路径"))
.and_then(|x| x.as_str())
.filter(|s| !s.is_empty());
// Minimal SVG so view/plot channel can embed the formula text.
let escaped = latex
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;");
let svg = format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"480\" height=\"64\" data-linalg=\"formula\">\
<text x=\"12\" y=\"40\" font-size=\"18\" font-family=\"serif\">{escaped}</text></svg>"
);
let _ = record_plot(&svg, path);
let mut out = e.to_value()?;
if let Some(obj) = out.as_object_mut() {
obj.insert("shown".into(), json!(true));
obj.insert("ascii".into(), json!(ascii));
obj.insert("latex".into(), json!(latex));
}
Ok(out)
});
la_ffi!(linalg_from_formula, |args: &Value| {
let f = args
.get("formula")
.or_else(|| args.get("公式"))
.or_else(|| args.get("expr"))
.or_else(|| args.get("式"))
.ok_or_else(|| "missing `formula`".to_string())?;
wrap(coerce_expr(f)?)
});
la_ffi!(linalg_explicit, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"]).or_else(|_| {
let f = args
.get("formula")
.or_else(|| args.get("公式"))
.ok_or_else(|| "missing `expr`".to_string())?;
coerce_expr(f)
})?;
let e = display_expr(args, e)?;
let m = dense::explicit(&e)?;
dense::to_dense_value(&m)
});
la_ffi!(linalg_det, |args: &Value| {
let m = dense_arg(args, &["expr", "a", "matrix", "式", "矩阵"])?;
Ok(json!(dense::det(&m)?))
});
la_ffi!(linalg_trace, |args: &Value| {
let m = dense_arg(args, &["expr", "a", "matrix", "式", "矩阵"])?;
Ok(json!(dense::trace(&m)?))
});
la_ffi!(linalg_solve, |args: &Value| {
let b = dense_arg(args, &["b", "B", "rhs", "右"])?;
if let Some(f) = args
.get("factor")
.or_else(|| args.get("分解"))
.filter(|v| !v.is_null())
{
let x = factor::solve_lu_factor(f, &b)?;
return dense::to_dense_value(&x).map(|xv| {
json!({
"_type": "linalg_solve",
"method": "lu",
"x": xv,
})
});
}
let a = dense_arg(args, &["a", "A", "matrix", "矩阵", "左"])?;
dense::solve_result(&a, &b)
});
la_ffi!(linalg_matmul, |args: &Value| {
let a = dense_arg(args, &["a", "左"])?;
let b = dense_arg(args, &["b", "右"])?;
dense::to_dense_value(&dense::matmul(&a, &b)?)
});
la_ffi!(linalg_kron, |args: &Value| {
let a = arg_expr_opt(args, &["a", "左"])?;
let b = arg_expr_opt(args, &["b", "右"])?;
wrap(matexpr::kron(a, b)?)
});
la_ffi!(linalg_block, |args: &Value| {
let blocks_v = args
.get("blocks")
.or_else(|| args.get("块"))
.ok_or_else(|| "missing `blocks`".to_string())?;
let grid = blocks_v
.as_array()
.ok_or_else(|| "`blocks` must be a list of rows".to_string())?;
let mut blocks = Vec::new();
for row in grid {
let cells = row
.as_array()
.ok_or_else(|| "each block row must be a list".to_string())?;
let mut r = Vec::new();
for c in cells {
r.push(coerce_expr(c)?);
}
blocks.push(r);
}
wrap(matexpr::block(blocks)?)
});
la_ffi!(linalg_collapse, |args: &Value| {
let e = arg_expr_opt(args, &["expr", "a", "式"])?;
wrap(matexpr::collapse(e)?)
});
la_ffi!(linalg_factorize, |args: &Value| {
let m = dense_arg(args, &["matrix", "expr", "a", "矩阵", "式"])?;
let kind = args
.get("kind")
.or_else(|| args.get("类型"))
.and_then(|x| x.as_str())
.unwrap_or("lu");
factor::factorize(&m, kind)
});
la_ffi!(linalg_draw, |args: &Value| {
let factor_v = args
.get("factor")
.or_else(|| args.get("分解"))
.or_else(|| args.get("expr"))
.or_else(|| args.get("matrix"))
.or_else(|| args.get("式"))
.or_else(|| args.get("矩阵"))
.ok_or_else(|| "missing `factor`".to_string())?;
let kind = args
.get("kind")
.or_else(|| args.get("类型"))
.and_then(|x| x.as_str());
let path = args
.get("path")
.or_else(|| args.get("路径"))
.and_then(|x| x.as_str())
.filter(|s| !s.is_empty());
let theme = args
.get("theme")
.or_else(|| args.get("主题"))
.and_then(|x| x.as_str())
.map(draw::ThemeName::parse)
.transpose()?
.unwrap_or(draw::ThemeName::Light);
let svg = draw::draw_factor(factor_v, kind, path, theme)?;
let _ = record_plot(&svg, path);
Ok(json!({
"_type": "linalg_svg",
"kind": kind
.or_else(|| factor_v.get("kind").and_then(|k| k.as_str()))
.unwrap_or("structure"),
"theme": theme.as_str(),
"svg": svg,
}))
});
la_ffi!(linalg_lstsq, |args: &Value| {
let a = dense_arg(args, &["a", "A", "matrix", "矩阵", "左"])?;
let b = dense_arg(args, &["b", "B", "rhs", "右"])?;
metrics::lstsq(&a, &b)
});
la_ffi!(linalg_norm, |args: &Value| {
let ord = args
.get("ord")
.or_else(|| args.get("order"))
.or_else(|| args.get("范数"))
.and_then(|x| x.as_str())
.unwrap_or("fro");
if let Some(v) = args
.get("expr")
.or_else(|| args.get("a"))
.or_else(|| args.get("matrix"))
.or_else(|| args.get("式"))
.or_else(|| args.get("矩阵"))
{
if v.get("dtype").and_then(|d| d.as_str()) == Some("complex")
|| complex::from_value(v).is_ok()
{
if !matches!(ord, "fro" | "frobenius" | "f" | "") {
return Err("complex norm only supports fro for now".into());
}
let cm = complex::from_value(v).or_else(|_| {
v.get("data")
.ok_or_else(|| "missing complex data".to_string())
.and_then(complex::from_data)
})?;
return Ok(json!(complex::frobenius_norm(&cm)?));
}
}
let m = dense_arg(args, &["expr", "a", "matrix", "式", "矩阵"])?;
Ok(json!(metrics::norm(&m, ord)?))
});
la_ffi!(linalg_cond, |args: &Value| {
let m = dense_arg(args, &["expr", "a", "matrix", "式", "矩阵"])?;
metrics::cond(&m)
});
la_ffi!(linalg_rank, |args: &Value| {
let m = dense_arg(args, &["expr", "a", "matrix", "式", "矩阵"])?;
Ok(json!(metrics::rank(&m)?))
});
fn dense_arg(args: &Value, keys: &[&str]) -> Result<dense::Mat, String> {
for k in keys {
if let Some(v) = args.get(*k) {
if v.is_null() {
continue;
}
if v.get("dtype").and_then(|d| d.as_str()) == Some("complex") {
return Err("real dense op got dtype=complex (use norm fro on complex; factorize/solve stay real)".into());
}
if let Ok(e) = Expr::from_value(v) {
return dense::explicit(&e);
}
return dense::from_value(v);
}
}
Err(format!("missing dense matrix among {:?}", keys))
}
#[no_mangle]
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
ABI_VERSION
}
#[no_mangle]
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
#[no_mangle]
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
if host.is_null() {
return 1;
}
let host = &*host;
HOST_ALLOC = host.alloc;
HOST_FREE = host.free;
HOST_QUERY = host.host_query;
HOST_USERDATA = host.userdata;
let register = match host.register_fn {
Some(f) => f,
None => return 1,
};
let regs = [
("linalg_ping", "", linalg_ping as PluginFn),
("linalg_symbol", "name,rows,cols", linalg_symbol as PluginFn),
("linalg_eye", "n", linalg_eye as PluginFn),
("linalg_zeros", "rows,cols", linalg_zeros as PluginFn),
("linalg_from_list", "data", linalg_from_list as PluginFn),
("linalg_declare", "table,type_tag", linalg_declare as PluginFn),
("linalg_from_formula", "formula", linalg_from_formula as PluginFn),
("linalg_mul", "a,b", linalg_mul as PluginFn),
("linalg_add", "a,b", linalg_add as PluginFn),
("linalg_sub", "a,b", linalg_sub as PluginFn),
("linalg_transpose", "expr", linalg_transpose as PluginFn),
("linalg_inv", "expr", linalg_inv as PluginFn),
("linalg_simplify", "expr", linalg_simplify as PluginFn),
("linalg_ascii", "expr,raw", linalg_ascii as PluginFn),
("linalg_latex", "expr,raw", linalg_latex as PluginFn),
("linalg_shape", "expr", linalg_shape as PluginFn),
("linalg_show", "expr,path,raw", linalg_show as PluginFn),
("linalg_explicit", "expr,raw", linalg_explicit as PluginFn),
("linalg_det", "expr", linalg_det as PluginFn),
("linalg_trace", "expr", linalg_trace as PluginFn),
("linalg_solve", "a,b,factor", linalg_solve as PluginFn),
("linalg_matmul", "a,b", linalg_matmul as PluginFn),
("linalg_kron", "a,b", linalg_kron as PluginFn),
("linalg_block", "blocks", linalg_block as PluginFn),
("linalg_collapse", "expr", linalg_collapse as PluginFn),
("linalg_factorize", "matrix,kind", linalg_factorize as PluginFn),
("linalg_draw", "factor,kind,path,theme", linalg_draw as PluginFn),
("linalg_lstsq", "a,b", linalg_lstsq as PluginFn),
("linalg_norm", "expr,ord", linalg_norm as PluginFn),
("linalg_cond", "expr", linalg_cond as PluginFn),
("linalg_rank", "expr", linalg_rank as PluginFn),
];
for (name, params, f) in regs {
let c_name = CString::new(name).unwrap();
let c_params = CString::new(params).unwrap();
if register(host.userdata, c_name.as_ptr(), c_params.as_ptr(), f) != 0 {
return 1;
}
}
0
}
File diff suppressed because it is too large. Load diff
+194
View File
@@ -0,0 +1,194 @@
//! Real dense metrics: lstsq, norm, cond, rank (L6).
use serde_json::{json, Value};
use crate::dense::{self, Mat};
use crate::factor;
/// Least squares `min ||A x - b||_2`. Tall: thin QR; wide/square: SVD pseudoinverse.
pub fn lstsq(a: &Mat, b: &Mat) -> Result<Value, String> {
let (m, n) = dense::shape(a)?;
let (br, bc) = dense::shape(b)?;
if br != m {
return Err(format!("lstsq: A is {m}×{n} but b has {br} rows"));
}
factor::check_side_limit(m.max(n), "lstsq")?;
let (x, residual_fro, method) = if m >= n {
lstsq_qr(a, b)?
} else {
lstsq_svd(a, b)?
};
let residual_fro = if bc == 1 {
residual_fro
} else {
// recompute full residual for multi-RHS
let ax = dense::matmul(a, &x)?;
frobenius_diff(&ax, b)
};
Ok(json!({
"_type": "linalg_lstsq",
"method": method,
"x": dense::to_dense_value(&x)?,
"residual_fro": residual_fro,
}))
}
fn lstsq_qr(a: &Mat, b: &Mat) -> Result<(Mat, f64, &'static str), String> {
let (q, r) = factor::qr_decomp(a)?;
let qt = dense::transpose(&q);
let qtb = dense::matmul(&qt, b)?;
let x = dense::solve(&r, &qtb)?;
let ax = dense::matmul(a, &x)?;
Ok((x, frobenius_diff(&ax, b), "qr"))
}
fn lstsq_svd(a: &Mat, b: &Mat) -> Result<(Mat, f64, &'static str), String> {
let (m, n) = dense::shape(a)?;
let (_, bc) = dense::shape(b)?;
let (u, s, vt) = factor::svd_decomp(a)?;
let rank = n.min(m);
let mut s_inv = vec![0.0; rank];
let smax = s.first().copied().unwrap_or(0.0);
let tol = smax * (m.max(n) as f64) * f64::EPSILON * 100.0;
for i in 0..rank {
if s[i] > tol {
s_inv[i] = 1.0 / s[i];
}
}
// x = V Σ⁺ Uᵀ b ; U is m×rank, Vt is rank×n
let ut = dense::transpose(&u);
let utb = dense::matmul(&ut, b)?; // rank×bc
let mut mid = dense::zeros(rank, bc);
for i in 0..rank {
for j in 0..bc {
mid[i][j] = s_inv[i] * utb[i][j];
}
}
let v = dense::transpose(&vt); // n×rank
let x = dense::matmul(&v, &mid)?;
let ax = dense::matmul(a, &x)?;
Ok((x, frobenius_diff(&ax, b), "svd"))
}
fn frobenius_diff(a: &Mat, b: &Mat) -> f64 {
let mut s = 0.0;
for (ra, rb) in a.iter().zip(b.iter()) {
for (&x, &y) in ra.iter().zip(rb.iter()) {
let d = x - y;
s += d * d;
}
}
s.sqrt()
}
/// Matrix norm: `fro` (default), `1`, `inf`, `2`.
pub fn norm(m: &Mat, ord: &str) -> Result<f64, String> {
let (rows, cols) = dense::shape(m)?;
match ord.trim().to_ascii_lowercase().as_str() {
"fro" | "frobenius" | "f" | "" => {
let mut s = 0.0;
for row in m {
for &v in row {
s += v * v;
}
}
Ok(s.sqrt())
}
"1" | "one" => {
let mut best: f64 = 0.0;
for j in 0..cols {
let mut col = 0.0;
for i in 0..rows {
col += m[i][j].abs();
}
best = best.max(col);
}
Ok(best)
}
"inf" | "infty" | "∞" => {
let mut best: f64 = 0.0;
for row in m {
let mut r = 0.0;
for &v in row {
r += v.abs();
}
best = best.max(r);
}
Ok(best)
}
"2" | "spectral" => {
let (_, s, _) = factor::svd_decomp(m)?;
Ok(s.first().copied().unwrap_or(0.0))
}
other => Err(format!(
"unknown norm `{other}` (want fro|1|inf|2)"
)),
}
}
/// 2-norm condition number σ_max / σ_min (∞ if rank-deficient).
pub fn cond(m: &Mat) -> Result<Value, String> {
let (rows, cols) = dense::shape(m)?;
let (_, s, _) = factor::svd_decomp(m)?;
let smax = s.first().copied().unwrap_or(0.0);
let smin = s
.iter()
.copied()
.filter(|&x| x > 0.0)
.last()
.unwrap_or(0.0);
let tol = smax * (rows.max(cols) as f64) * f64::EPSILON * 100.0;
let (value, infinite) = if smin <= tol {
(f64::INFINITY, true)
} else {
(smax / smin, false)
};
Ok(json!({
"_type": "linalg_cond",
"ord": 2,
"value": if infinite { Value::Null } else { json!(value) },
"infinite": infinite,
"sigma_max": smax,
"sigma_min": smin,
}))
}
/// Numerical rank via SVD.
pub fn rank(m: &Mat) -> Result<usize, String> {
let (rows, cols) = dense::shape(m)?;
let (_, s, _) = factor::svd_decomp(m)?;
let smax = s.first().copied().unwrap_or(0.0);
let tol = smax * (rows.max(cols) as f64) * f64::EPSILON * 100.0;
Ok(s.iter().filter(|&&x| x > tol).count())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lstsq_line_fit() {
// points (0,1), (1,2), (2,2) → line ≈ 1.1667 + 0.5 t
let a = vec![vec![1.0, 0.0], vec![1.0, 1.0], vec![1.0, 2.0]];
let b = vec![vec![1.0], vec![2.0], vec![2.0]];
let out = lstsq(&a, &b).unwrap();
let x = dense::from_value(out.get("x").unwrap()).unwrap();
assert!((x[0][0] - 7.0 / 6.0).abs() < 1e-9);
assert!((x[1][0] - 0.5).abs() < 1e-9);
}
#[test]
fn fro_identity() {
let i = dense::eye(3);
assert!((norm(&i, "fro").unwrap() - 3f64.sqrt()).abs() < 1e-12);
}
#[test]
fn cond_diag() {
let m = vec![vec![2.0, 0.0], vec![0.0, 1.0]];
let c = cond(&m).unwrap();
assert_eq!(c["infinite"], false);
assert!((c["value"].as_f64().unwrap() - 2.0).abs() < 1e-9);
assert_eq!(rank(&m).unwrap(), 2);
}
}
+3 -1
View File
@@ -12,7 +12,9 @@
| [`stdlib/`](stdlib/) | **标准库**(导入方式 + text/表/文件/时间/系统/json/网络/数学/外联/插件/**自写回**/**子任务**;v0.1.2+ 内置于 CLI) | | [`stdlib/`](stdlib/) | **标准库**(导入方式 + text/表/文件/时间/系统/json/网络/数学/外联/插件/**自写回**/**子任务**;v0.1.2+ 内置于 CLI) |
| [`features/`](features/) | 特性迭代(含对象、官方 `ext/`) | | [`features/`](features/) | 特性迭代(含对象、官方 `ext/`) |
官方扩展(非 stdlib):`ext/llm`、`ext/agent` — 见 `features/05-extensions.mq.md` / `05-扩展.mq.md` 与 `doc/design/ext-*.md`。 官方扩展(非 stdlib):`ext/llm`、`ext/agent`、`ext/web`、`ext/quantum`、`ext/linalg` — 见 `features/05-*.mq.md`、`06-*.mq.md`、`07-linalg.mq.md` / `07-线性代数.mq.md` 与 `doc/design/ext-*.md`。
中层标准库(Mid):`08`–`12`(re/encoding · path/fs · hash/secrets · csv/text · list/cli;及对应中文页)。
**不放** `errors/`:失败样例只在 `tests/errors/`。 **不放** `errors/`:失败样例只在 `tests/errors/`。
+35
View File
@@ -0,0 +1,35 @@
---
title: How to write a linear-algebra formula document
description: ext/linalg formula-doc — declare, infix, run-implies-correct
import la:ext/linalg/linalg.mq.md
---
# Formula documents with ext/linalg
Not stdlib. Install: `marqdo ext add linalg` (or build `marqdo_plugin_linalg`).
**Idea:** if the `.mq.md` runs and the asserted form matches, the identity holds under the library rules. Algebra reads like a lecture; `factorize` / `lstsq` / `draw` stay explicit calculation steps.
# main
Declare symbols in a table, multiply, transpose — `T_ascii` is one-step display (auto-simplify).
`shapes` =
| @ | name | rows | cols |
|---|------|------|------|
| 1 | A | 2 | 3 |
| 2 | B | 3 | 2 |
*`env` = > la.declare table=`shapes`*
*`A` = env[^A]*
*`B` = env[^B]*
*`P` = `A` * `B`*
*`t` = > `P`.T_ascii*
> print text=`t`
1. `t` == "B^T*A^T"
> print text=formula-doc-ok
2. *
> print text=formula-doc-fail
> print text=see-examples-linalg-transpose
+35
View File
@@ -0,0 +1,35 @@
---
title: 如何写线性代数公式文档
description: ext/linalg 公式文档面 — 声明、中缀、跑通即正确
导入 la:ext/linalg/线性代数.mq.md
---
# 用 ext/linalg 写公式文档
可选扩展(非 stdlib)。安装:`marqdo ext add linalg`(中文 id:`线性代数`)。
**思想:** `.mq.md` 能跑通且断言的规范形一致,则在库规则下该恒等式成立。代数叙述像讲义;`分解` / `最小二乘` / `绘图` 才是显式计算步骤。
# main
用表声明符号,再乘与转置 — `转置文本` 一步展示(默认化简)。
`形状` =
| @ | 名 | 行 | 列 |
|---|----|----|----|
| 1 | A | 2 | 3 |
| 2 | B | 3 | 2 |
*`环境` = > la.声明 表=`形状`*
*`A` = 环境[^A]*
*`B` = 环境[^B]*
*`P` = `A` * `B`*
*`t` = > `P`.转置文本*
> 打印 内容=`t`
1. `t` == "B^T*A^T"
> 打印 内容=formula-doc-ok
2. *
> 打印 内容=formula-doc-fail
> 打印 内容=see-examples-linalg-transpose
+23
View File
@@ -0,0 +1,23 @@
---
title: Mid stdlib — regex and encoding
description: lib/re + lib/encoding without plugins (Mid M1)
import re:lib/re.mq.md
import enc:lib/encoding.mq.md
---
# Regex and encoding in lib/
Mid-tier stdlib: everyday primitives stay in `lib/`, not ABI plugins.
# main
*`ok` = > re.is_match text="user@example.com" pattern="^[^@]+@[^@]+$"*
*`tok` = > enc.base64_encode text="marqdo"*
*`hex` = > enc.hex_encode text="ok"*
> print text=`ok`
> print text=`tok`
> print text=`hex`
1. `ok`
> print text=mid-m1-ok
2. *
> print text=mid-m1-fail
+23
View File
@@ -0,0 +1,23 @@
---
title: 中层标准库 — 正则与编解码
description: lib/正则 + lib/编码,无需插件(中层 M1)
导入 正则:lib/正则.mq.md
导入 编码:lib/编码.mq.md
---
# 标准库中的正则与编解码
中层标准库:日常原语进 `lib/`,不必 ABI 插件。
# main
*`ok` = > 正则.匹配 内容="user@example.com" 模式="^[^@]+@[^@]+$"*
*`tok` = > 编码.基64编码 内容="marqdo"*
*`hex` = > 编码.十六进制编码 内容="ok"*
> 打印 内容=`ok`
> 打印 内容=`tok`
> 打印 内容=`hex`
1. `ok`
> 打印 内容=mid-m1-ok
2. *
> 打印 内容=mid-m1-fail
+27
View File
@@ -0,0 +1,27 @@
---
title: Mid stdlib — path and fs
description: lib/path + fs copy/move/temp (Mid M2)
import path:lib/path.mq.md
import fs:lib/fs.mq.md
---
# Path algebra and file mid-layer
Join and normalize without plugins; copy/move stay sandboxed under the program directory.
# main
*`p` = > path.join a="out" b="demo.txt"*
*`n` = > path.normalize path="out/./x/../demo.txt"*
> fs.write_text path=`p` text="path-m2"
> fs.copy_file src=`p` dest="out/demo-copy.txt"
*`body` = > fs.read_text path="out/demo-copy.txt"*
> print text=`p`
> print text=`n`
> print text=`body`
1. `body` == "path-m2"
> print text=mid-m2-ok
2. *
> print text=mid-m2-fail
> fs.remove path=`p`
> fs.remove path="out/demo-copy.txt"
+27
View File
@@ -0,0 +1,27 @@
---
title: 中层标准库 — 路径与文件
description: lib/路径 + 文件复制/移动/临时(中层 M2)
导入 路径:lib/路径.mq.md
导入 文件:lib/文件.mq.md
---
# 路径代数与文件中层
无插件拼接/规范化;复制/移动仍落在程序目录沙箱内。
# main
*`p` = > 路径.拼接 甲="out" 乙="demo.txt"*
*`n` = > 路径.规范化 路径="out/./x/../demo.txt"*
> 文件.写文本 路径=`p` 内容="path-m2"
> 文件.复制文件 源=`p` 目标="out/demo-copy.txt"
*`body` = > 文件.读文本 路径="out/demo-copy.txt"*
> 打印 内容=`p`
> 打印 内容=`n`
> 打印 内容=`body`
1. `body` == "path-m2"
> 打印 内容=mid-m2-ok
2. *
> 打印 内容=mid-m2-fail
> 文件.删除 路径=`p`
> 文件.删除 路径="out/demo-copy.txt"
+22
View File
@@ -0,0 +1,22 @@
---
title: Mid stdlib — hash and secrets
description: lib/hash + lib/secrets without plugins (Mid M3)
import hash:lib/hash.mq.md
import sec:lib/secrets.mq.md
---
# Digests and secure tokens
Integrity digests and cryptographic tokens stay in `lib/` — do not use `math.random` for secrets.
# main
*`d` = > hash.sha256 text="marqdo"*
*`t` = > sec.token_hex n=8*
*`L` = > len `t`*
> print text=`d`
> print text=`L`
1. `L` == 16
> print text=mid-m3-ok
2. *
> print text=mid-m3-fail
+22
View File
@@ -0,0 +1,22 @@
---
title: 中层标准库 — 哈希与机密
description: lib/哈希 + lib/机密,无需插件(中层 M3)
导入 哈希:lib/哈希.mq.md
导入 机密:lib/机密.mq.md
---
# 摘要与安全令牌
完整性摘要与加密令牌进 `lib/` — 勿用 `math.random` 生成令牌。
# main
*`d` = > 哈希.哈希256 内容="marqdo"*
*`t` = > 机密.十六进制令牌 字节数=8*
*`L` = > 长度 `t`*
> 打印 内容=`d`
> 打印 内容=`L`
1. `L` == 16
> 打印 内容=mid-m3-ok
2. *
> 打印 内容=mid-m3-fail
+24
View File
@@ -0,0 +1,24 @@
---
title: Mid stdlib — csv and text
description: lib/csv + thickened lib/text (Mid M4)
import csv:lib/csv.mq.md
import text:lib/text.mq.md
---
# CSV cleaning with text helpers
Parse a tiny CSV, normalize a field, print — no plugins.
# main
*raw = "name,city\nAda,london\nBob,PARIS\n"*
*rows = > csv.parse text=`raw`*
*r0 = > at value=`rows` index=0*
*city = > text.to_upper text=`r0`[^city]*
> print text=`r0`[^name]
> print text=`city`
*n = > len `rows`*
1. `n` == 2
> print text=mid-m4-ok
2. *
> print text=mid-m4-fail
+24
View File
@@ -0,0 +1,24 @@
---
title: 中层标准库 — 逗号表与文本
description: lib/逗号表 + 加厚 lib/文本(中层 M4)
导入 逗号表:lib/逗号表.mq.md
导入 文本:lib/文本.mq.md
---
# 用文本助手清洗 CSV
解析小表、规范化字段、打印 — 无需插件。
# main
*raw = "name,city\nAda,london\nBob,PARIS\n"*
*rows = > 逗号表.解析 内容=`raw`*
*r0 = > at value=`rows` index=0*
*city = > 文本.大写 内容=`r0`[^city]*
> 打印 内容=`r0`[^name]
> 打印 内容=`city`
*n = > 长度 `rows`*
1. `n` == 2
> 打印 内容=mid-m4-ok
2. *
> 打印 内容=mid-m4-fail
+27
View File
@@ -0,0 +1,27 @@
---
title: Mid stdlib — lists and cli
description: table sort/unique/zip + cli.parse (Mid M5)
import table:lib/table.mq.md
import csv:lib/csv.mq.md
import cli:lib/cli.mq.md
---
# Sort CSV rows and parse flags
Zero plugins: parse CSV, sort by key, parse `--input`.
# main
*raw = "name,score\nbob,2\nalice,10\n"*
*rows = > csv.parse text=`raw`*
*rows = > table.sort_by list=`rows` key="name"*
*r0 = > at value=`rows` index=0*
> print text=`r0`[^name]
*args = > split value="--input,scores.csv,--pretty" sep=","*
*flags = > cli.parse args=`args`*
> print text=`flags`[^input]
1. `flags`[^pretty]
> print text=mid-m5-ok
2. *
> print text=mid-m5-fail
+27
View File
@@ -0,0 +1,27 @@
---
title: 中层标准库 — 列表与命令行
description: 表排序/去重 + 命令行解析(中层 M5)
导入 表:lib/表.mq.md
导入 逗号表:lib/逗号表.mq.md
导入 命令行:lib/命令行.mq.md
---
# 排序 CSV 并解析参数
零插件:解析 CSV、按键排序、解析 `--input`。
# main
*raw = "name,score\nbob,2\nalice,10\n"*
*rows = > 逗号表.解析 内容=`raw`*
*rows = > 表.按键排序 list=`rows` key="name"*
*r0 = > at value=`rows` index=0*
> 打印 内容=`r0`[^name]
*args = > split value="--input,scores.csv,--pretty" sep=","*
*flags = > 命令行.解析 参数=`args`*
> 打印 内容=`flags`[^input]
1. `flags`[^pretty]
> 打印 内容=mid-m5-ok
2. *
> 打印 内容=mid-m5-fail
+24
View File
@@ -0,0 +1,24 @@
---
title: Mid stdlib — log and uuid
description: Level-filtered logs + UUID v4 (Mid M6)
import log:lib/log.mq.md
import uuid:lib/uuid.mq.md
---
# Observable scripts without plugins
Default log level is info; raise to debug when you need more. Mint ids with uuid.v4.
# main
> log.info text="mid-m6-start"
> log.debug text="skipped-by-default"
> log.set_level level="debug"
> log.debug text="now-visible"
*id = > uuid.v4*
*n = > len `id`*
> print text=`n`
+24
View File
@@ -0,0 +1,24 @@
---
title: 中层标准库 — 日志与标识
description: 按级别过滤的日志 + UUID 版本4(中层 M6)
导入 日志:lib/日志.mq.md
导入 标识:lib/标识.mq.md
---
# 无插件的可观测脚本
默认日志级别为 info;需要细节时再调到 debug。用 标识.版本4 生成 id。
# main
> 日志.信息 内容="中层-m6-开始"
> 日志.调试 内容="默认会跳过"
> 日志.设级别 级别="debug"
> 日志.调试 内容="现在可见"
*id = > 标识.版本4*
*n = > 长度 `id`*
> 打印 内容=`n`
+2 -2
View File
@@ -4,8 +4,8 @@ set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)" ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT" cd "$ROOT"
# CLI + native plugins used by public demos (ext/quantum). # CLI + native plugins used by public demos (ext/quantum, ext/linalg).
cargo build --release -p marqdo -p marqdo_plugin_quantum cargo build --release -p marqdo -p marqdo_plugin_quantum -p marqdo_plugin_linalg
BIN=./target/release/marqdo BIN=./target/release/marqdo
while IFS= read -r -d '' f; do while IFS= read -r -d '' f; do
+66 -15
View File
@@ -30,6 +30,8 @@ struct Frame {
fn_idx: usize, fn_idx: usize,
ip: usize, ip: usize,
slots: Vec<Value>, slots: Vec<Value>,
/// When set, retag matrix-like return to this `_type` (method chaining).
retag_matrix: Option<String>,
} }
impl Vm { impl Vm {
@@ -115,6 +117,7 @@ impl Vm {
fn_idx: entry, fn_idx: entry,
ip: 0, ip: 0,
slots: vec![Value::None; program.functions[entry].locals.len().max(1)], slots: vec![Value::None; program.functions[entry].locals.len().max(1)],
retag_matrix: None,
}]; }];
self.host self.host
.push_call_frame(&program.functions[entry].name.clone()); .push_call_frame(&program.functions[entry].name.clone());
@@ -182,10 +185,57 @@ impl Vm {
Op::Add => { Op::Add => {
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(add(a, b).map_err(|m| self.err_at(span, m))?); if let Some(v) = crate::linalg_ops::try_matrix_binary(
&mut self.host,
crate::ast::BinaryOp::Add,
&a,
&b,
)
.map_err(|m| self.err_at(span, m))?
{
stack.push(v);
} else {
stack.push(add(a, b).map_err(|m| self.err_at(span, m))?);
}
}
Op::Sub => {
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
if let Some(v) = crate::linalg_ops::try_matrix_binary(
&mut self.host,
crate::ast::BinaryOp::Sub,
&a,
&b,
)
.map_err(|m| self.err_at(span, m))?
{
stack.push(v);
} else {
match (a, b) {
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x - y)),
_ => return Err(self.err_at(span, "`-` needs ints")),
}
}
}
Op::Mul => {
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
if let Some(v) = crate::linalg_ops::try_matrix_binary(
&mut self.host,
crate::ast::BinaryOp::Mul,
&a,
&b,
)
.map_err(|m| self.err_at(span, m))?
{
stack.push(v);
} else {
match (a, b) {
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x * y)),
_ => return Err(self.err_at(span, "`*` needs ints")),
}
}
} }
Op::Sub => bin_int(&mut stack, |a, b| a - b).map_err(|m| self.err_at(span, m))?,
Op::Mul => bin_int(&mut stack, |a, b| a * b).map_err(|m| self.err_at(span, m))?,
Op::Div => { Op::Div => {
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
@@ -383,6 +433,7 @@ impl Vm {
fn_idx: fid, fn_idx: fid,
ip: 0, ip: 0,
slots, slots,
retag_matrix: None,
}); });
self.host.push_call_frame(&fname); self.host.push_call_frame(&fname);
self.host.push_call_site_line(span.line); self.host.push_call_site_line(span.line);
@@ -457,11 +508,17 @@ impl Vm {
slots[i] = recv.clone(); slots[i] = recv.clone();
} }
} }
let retag_matrix = if crate::linalg_ops::is_matrix_value(&recv) {
Some(type_name.clone())
} else {
None
};
let fname = callee.name.clone(); let fname = callee.name.clone();
frames.push(Frame { frames.push(Frame {
fn_idx: method_fid, fn_idx: method_fid,
ip: 0, ip: 0,
slots, slots,
retag_matrix,
}); });
self.host.push_call_frame(&fname); self.host.push_call_frame(&fname);
self.host.push_call_site_line(span.line); self.host.push_call_site_line(span.line);
@@ -551,15 +608,20 @@ impl Vm {
fn_idx: fid, fn_idx: fid,
ip: 0, ip: 0,
slots, slots,
retag_matrix: None,
}); });
self.host.push_call_frame(&fname); self.host.push_call_frame(&fname);
self.host.push_call_site_line(span.line); self.host.push_call_site_line(span.line);
} }
Op::Return => { Op::Return => {
let ret = pop(&mut stack).unwrap_or(Value::None); let ret = pop(&mut stack).unwrap_or(Value::None);
frames.pop(); let frame = frames.pop();
self.host.pop_call_frame(); self.host.pop_call_frame();
self.host.pop_call_site_line(); self.host.pop_call_site_line();
let ret = match frame.and_then(|f| f.retag_matrix) {
Some(ty) => crate::linalg_ops::retag_matrix_like(&ty, ret),
None => ret,
};
if frames.is_empty() { if frames.is_empty() {
return Ok(ret); return Ok(ret);
} }
@@ -610,17 +672,6 @@ fn add(a: Value, b: Value) -> Result<Value, String> {
} }
} }
fn bin_int(stack: &mut Vec<Value>, f: impl Fn(i64, i64) -> i64) -> Result<(), String> {
let b = pop(stack)?;
let a = pop(stack)?;
match (a, b) {
(Value::Int(x), Value::Int(y)) => {
stack.push(Value::Int(f(x, y)));
Ok(())
}
_ => Err("binary op needs ints".into()),
}
}
fn cmp(stack: &mut Vec<Value>, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<(), String> { fn cmp(stack: &mut Vec<Value>, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<(), String> {
let b = pop(stack)?; let b = pop(stack)?;
+79 -23
View File
@@ -42,6 +42,12 @@ pub const CATALOG: &[ExtPackage] = &[
mq_files: &["quantum/quantum.mq.md", "quantum/量子.mq.md"], mq_files: &["quantum/quantum.mq.md", "quantum/量子.mq.md"],
native_crate: Some("marqdo_plugin_quantum"), native_crate: Some("marqdo_plugin_quantum"),
}, },
ExtPackage {
id: "linalg",
description: "Formula-first linear algebra (ext/linalg) — MatExpr simplify/display",
mq_files: &["linalg/linalg.mq.md", "linalg/线性代数.mq.md"],
native_crate: Some("marqdo_plugin_linalg"),
},
]; ];
pub fn find_package(id: &str) -> Option<&'static ExtPackage> { pub fn find_package(id: &str) -> Option<&'static ExtPackage> {
@@ -93,6 +99,7 @@ pub fn native_short_name(crate_or_id: &str) -> &str {
"marqdo_plugin_agent" | "agent" => "agent", "marqdo_plugin_agent" | "agent" => "agent",
"marqdo_plugin_web" | "web" => "web", "marqdo_plugin_web" | "web" => "web",
"marqdo_plugin_quantum" | "quantum" => "quantum", "marqdo_plugin_quantum" | "quantum" => "quantum",
"marqdo_plugin_linalg" | "linalg" => "linalg",
other => other, other => other,
} }
} }
@@ -112,6 +119,7 @@ pub fn native_env_var(short: &str) -> &'static str {
match native_short_name(short) { match native_short_name(short) {
"web" => "MARQDO_WEB_PLUGIN", "web" => "MARQDO_WEB_PLUGIN",
"quantum" => "MARQDO_QUANTUM_PLUGIN", "quantum" => "MARQDO_QUANTUM_PLUGIN",
"linalg" => "MARQDO_LINALG_PLUGIN",
_ => "MARQDO_AGENT_PLUGIN", _ => "MARQDO_AGENT_PLUGIN",
} }
} }
@@ -173,8 +181,24 @@ fn find_source_file(name: &str) -> Result<PathBuf> {
return Ok(p); return Ok(p);
} }
} }
#[cfg(feature = "net-host")]
{
if crate::ext_fetch::downloads_enabled() {
match crate::ext_fetch::ensure_ext_source_tree() {
Ok(root) => {
let p = root.join(name);
if p.is_file() {
return Ok(p);
}
}
Err(e) => {
eprintln!("note: could not download ext sources: {e:#}");
}
}
}
}
bail!( bail!(
"cannot find source `{name}` for install (set MARQDO_EXT_SOURCE to an ext/ directory, or run from the Marqdo repo)" "cannot find source `{name}` for install (set MARQDO_EXT_SOURCE to an ext/ directory, run from the Marqdo repo, or use a release that publishes marqdo-*-ext.zip)"
); );
} }
@@ -240,31 +264,58 @@ fn find_native_plugin(short: &str) -> Result<PathBuf> {
) )
} }
/// Locate a built native plugin, or `cargo build -p …` once then look again. /// Locate a built native plugin: local artifact → GitHub Release prebuild → optional cargo build.
fn ensure_native_plugin(crate_name: &str) -> Result<PathBuf> { fn ensure_native_plugin(crate_name: &str) -> Result<PathBuf> {
let short = native_short_name(crate_name); let short = native_short_name(crate_name);
if let Ok(p) = find_native_plugin(short) { if let Ok(p) = find_native_plugin(short) {
return Ok(p); return Ok(p);
} }
println!( let lib_name = native_lib_filename(short);
"native plugin for `{short}` not found; building `{crate_name}` (debug)…"
); #[cfg(feature = "net-host")]
let status = std::process::Command::new("cargo") {
.args(["build", "-p", crate_name]) match crate::ext_fetch::download_native_plugin(short, &lib_name) {
.status() Ok(p) => return Ok(p),
.with_context(|| format!("spawn cargo build -p {crate_name}"))?; Err(e) => {
if !status.success() { eprintln!("note: prebuilt native download skipped/failed: {e:#}");
bail!( }
"cargo build -p {crate_name} failed (status {status}); build the plugin then re-run `marqdo ext add {short}`" }
);
} }
find_native_plugin(short).with_context(|| {
format!( #[cfg(feature = "net-host")]
"built `{crate_name}` but still cannot find {}; set {} to the .so/.dll path", let have_cargo = crate::ext_fetch::cargo_available();
native_lib_filename(short), #[cfg(not(feature = "net-host"))]
native_env_var(short) let have_cargo = true;
)
}) if have_cargo {
println!(
"native plugin for `{short}` not found; building `{crate_name}` (debug)…"
);
let status = std::process::Command::new("cargo")
.args(["build", "-p", crate_name])
.status()
.with_context(|| format!("spawn cargo build -p {crate_name}"))?;
if !status.success() {
bail!(
"cargo build -p {crate_name} failed (status {status}); \
install a prebuilt release asset or build the plugin then re-run `marqdo ext add {short}`"
);
}
return find_native_plugin(short).with_context(|| {
format!(
"built `{crate_name}` but still cannot find {}; set {} to the .so/.dll path",
lib_name,
native_env_var(short)
)
});
}
bail!(
"cannot find native plugin `{lib_name}` and Rust/cargo is not available. \
Install Marqdo from a GitHub Release that includes `marqdo-*-native-*.zip`, \
or set {} to a built plugin path.",
native_env_var(short)
)
} }
pub fn add_ext(id: &str) -> Result<()> { pub fn add_ext(id: &str) -> Result<()> {
@@ -372,7 +423,7 @@ pub fn remove_ext(id: &str) -> Result<()> {
/// Also falls back to cargo `target/{debug,release}` artifacts for local runs. /// Also falls back to cargo `target/{debug,release}` artifacts for local runs.
pub fn installed_native_path(name: &str) -> Option<PathBuf> { pub fn installed_native_path(name: &str) -> Option<PathBuf> {
let short = native_short_name(name); let short = native_short_name(name);
if !matches!(short, "agent" | "web" | "quantum") { if !matches!(short, "agent" | "web" | "quantum" | "linalg") {
return None; return None;
} }
let lib_name = native_lib_filename(short); let lib_name = native_lib_filename(short);
@@ -411,7 +462,12 @@ pub fn path_is_trusted_plugin(path: &Path) -> bool {
return true; return true;
} }
} }
for key in ["MARQDO_AGENT_PLUGIN", "MARQDO_WEB_PLUGIN", "MARQDO_QUANTUM_PLUGIN"] { for key in [
"MARQDO_AGENT_PLUGIN",
"MARQDO_WEB_PLUGIN",
"MARQDO_QUANTUM_PLUGIN",
"MARQDO_LINALG_PLUGIN",
] {
if let Ok(p) = env::var(key) { if let Ok(p) = env::var(key) {
let p = PathBuf::from(p); let p = PathBuf::from(p);
let p = p.canonicalize().unwrap_or(p); let p = p.canonicalize().unwrap_or(p);
@@ -420,7 +476,7 @@ pub fn path_is_trusted_plugin(path: &Path) -> bool {
} }
} }
} }
for short in ["agent", "web", "quantum"] { for short in ["agent", "web", "quantum", "linalg"] {
if let Ok(found) = find_native_plugin(short) { if let Ok(found) = find_native_plugin(short) {
let found = found.canonicalize().unwrap_or(found); let found = found.canonicalize().unwrap_or(found);
if found == path { if found == path {
+307
View File
@@ -0,0 +1,307 @@
//! Download official `ext/` L1 sources and native plugins from GitHub Releases.
//!
//! Enables `marqdo ext add web` without a local Rust toolchain or repo checkout.
//! Disable with `MARQDO_EXT_NO_DOWNLOAD=1`. Override tag with `MARQDO_EXT_VERSION=0.3.4`.
use std::env;
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
const REPO: &str = "cflmy/marqdo";
pub fn downloads_enabled() -> bool {
match env::var("MARQDO_EXT_NO_DOWNLOAD") {
Ok(v) => {
let v = v.trim();
!(v == "1" || v.eq_ignore_ascii_case("true") || v.eq_ignore_ascii_case("yes"))
}
Err(_) => true,
}
}
/// SemVer without leading `v` (matches release asset names).
pub fn release_version() -> String {
if let Ok(v) = env::var("MARQDO_EXT_VERSION") {
let v = v.trim().trim_start_matches('v');
if !v.is_empty() {
return v.to_string();
}
}
env!("CARGO_PKG_VERSION").to_string()
}
pub fn host_target_triple() -> Option<&'static str> {
if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
Some("x86_64-pc-windows-msvc")
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
Some("x86_64-unknown-linux-gnu")
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
Some("aarch64-apple-darwin")
} else if cfg!(all(target_os = "macos", target_arch = "x86_64")) {
Some("x86_64-apple-darwin")
} else {
None
}
}
fn cache_root() -> Result<PathBuf> {
let home = env::var_os("USERPROFILE")
.or_else(|| env::var_os("HOME"))
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."));
let dir = home.join(".marqdo").join("cache");
fs::create_dir_all(&dir)?;
Ok(dir)
}
fn user_agent() -> String {
format!("marqdo/{}", env!("CARGO_PKG_VERSION"))
}
fn download_to_file(url: &str, dest: &Path) -> Result<()> {
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent)?;
}
let tmp = dest.with_extension("download-tmp");
let resp = ureq::get(url)
.set("User-Agent", &user_agent())
.set("Accept", "application/octet-stream")
.call()
.with_context(|| format!("GET {url}"))?;
if !(200..300).contains(&resp.status()) {
bail!("download {url} returned HTTP {}", resp.status());
}
let mut reader = resp.into_reader();
let mut out = File::create(&tmp).with_context(|| format!("create {}", tmp.display()))?;
io::copy(&mut reader, &mut out).with_context(|| format!("write {}", tmp.display()))?;
out.flush()?;
drop(out);
if dest.exists() {
let _ = fs::remove_file(dest);
}
fs::rename(&tmp, dest).with_context(|| format!("rename {}", dest.display()))?;
Ok(())
}
fn extract_zip(zip_path: &Path, dest_dir: &Path) -> Result<()> {
fs::create_dir_all(dest_dir)?;
let file = File::open(zip_path).with_context(|| format!("open {}", zip_path.display()))?;
let mut archive = zip::ZipArchive::new(file)
.with_context(|| format!("read zip {}", zip_path.display()))?;
for i in 0..archive.len() {
let mut entry = archive
.by_index(i)
.with_context(|| format!("zip entry {i}"))?;
let name = entry.name().to_string();
if name.is_empty() {
continue;
}
if Path::new(&name)
.components()
.any(|c| matches!(c, std::path::Component::ParentDir))
{
bail!("refusing zip entry with ..: {name}");
}
let out_path = dest_dir.join(Path::new(&name));
if entry.is_dir() || name.ends_with('/') {
fs::create_dir_all(&out_path)?;
continue;
}
if let Some(parent) = out_path.parent() {
fs::create_dir_all(parent)?;
}
let mut outfile =
File::create(&out_path).with_context(|| format!("create {}", out_path.display()))?;
io::copy(&mut entry, &mut outfile)
.with_context(|| format!("extract {name}"))?;
}
Ok(())
}
fn release_asset_url(ver: &str, filename: &str) -> String {
format!("https://github.com/{REPO}/releases/download/v{ver}/{filename}")
}
/// Ensure L1 `ext/` tree is available (repo checkout or downloaded Release zip).
/// Returns a directory that contains `web/web.mq.md`, `ai/…`, etc.
pub fn ensure_ext_source_tree() -> Result<PathBuf> {
let ver = release_version();
let cache = cache_root()?.join(format!("ext-src-v{ver}"));
let marker = cache.join("web").join("web.mq.md");
if marker.is_file() {
return Ok(cache);
}
// Zip layout from CI: top-level `ext/…`
let nested = cache.join("ext").join("web").join("web.mq.md");
if nested.is_file() {
return Ok(cache.join("ext"));
}
if !downloads_enabled() {
bail!("extension sources not found and downloads disabled (MARQDO_EXT_NO_DOWNLOAD)");
}
let zip_name = format!("marqdo-{ver}-ext.zip");
let url = release_asset_url(&ver, &zip_name);
let zip_path = cache_root()?.join(&zip_name);
println!("downloading official ext sources ({url})…");
download_to_file(&url, &zip_path)?;
let stage = cache_root()?.join(format!("ext-src-v{ver}-extract"));
if stage.exists() {
let _ = fs::remove_dir_all(&stage);
}
extract_zip(&zip_path, &stage)?;
// Prefer `ext/` child if present
let src = if stage.join("ext").join("web").join("web.mq.md").is_file() {
stage.join("ext")
} else if stage.join("web").join("web.mq.md").is_file() {
stage.clone()
} else {
bail!(
"downloaded {zip_name} but could not find web/web.mq.md inside; check release assets"
);
};
if cache.exists() {
let _ = fs::remove_dir_all(&cache);
}
// Move/copy tree into stable cache path
copy_dir_recursive(&src, &cache)?;
let _ = fs::remove_dir_all(&stage);
if !cache.join("web").join("web.mq.md").is_file() {
bail!("failed to cache ext sources under {}", cache.display());
}
println!("cached ext sources at {}", cache.display());
Ok(cache)
}
fn copy_dir_recursive(src: &Path, dest: &Path) -> Result<()> {
fs::create_dir_all(dest)?;
for entry in fs::read_dir(src).with_context(|| format!("read {}", src.display()))? {
let entry = entry?;
let ty = entry.file_type()?;
let to = dest.join(entry.file_name());
if ty.is_dir() {
copy_dir_recursive(&entry.path(), &to)?;
} else if ty.is_file() {
if let Some(parent) = to.parent() {
fs::create_dir_all(parent)?;
}
fs::copy(entry.path(), &to)
.with_context(|| format!("copy {} → {}", entry.path().display(), to.display()))?;
}
}
Ok(())
}
/// Download prebuilt native plugins zip for this host; return path to `lib` file for `short`.
pub fn download_native_plugin(short: &str, lib_name: &str) -> Result<PathBuf> {
if !downloads_enabled() {
bail!("native plugin not found and downloads disabled (MARQDO_EXT_NO_DOWNLOAD)");
}
let Some(triple) = host_target_triple() else {
bail!(
"no prebuilt native plugins for this platform; install Rust and run `cargo build -p marqdo_plugin_{short}`"
);
};
let ver = release_version();
let zip_name = format!("marqdo-{ver}-native-{triple}.zip");
let url = release_asset_url(&ver, &zip_name);
let cache = cache_root()?.join(format!("native-v{ver}-{triple}"));
let lib_in_cache = cache.join("native").join(lib_name);
if lib_in_cache.is_file() {
return Ok(lib_in_cache);
}
// Also accept flat layout
let flat = cache.join(lib_name);
if flat.is_file() {
return Ok(flat);
}
let zip_path = cache_root()?.join(&zip_name);
println!("downloading prebuilt native plugins ({url})…");
download_to_file(&url, &zip_path).with_context(|| {
format!(
"failed to download prebuilt plugins for {triple}. \
Build locally with `cargo build --release -p marqdo_plugin_{short}`, \
or check https://github.com/{REPO}/releases/tag/v{ver}"
)
})?;
if cache.exists() {
let _ = fs::remove_dir_all(&cache);
}
fs::create_dir_all(&cache)?;
extract_zip(&zip_path, &cache)?;
if lib_in_cache.is_file() {
println!("cached {}", lib_in_cache.display());
return Ok(lib_in_cache);
}
if flat.is_file() {
return Ok(flat);
}
// Search recursively for the lib name
if let Some(found) = find_named_file(&cache, lib_name)? {
return Ok(found);
}
bail!(
"downloaded {zip_name} but missing {lib_name}; expected native/{lib_name} in the archive"
)
}
fn find_named_file(root: &Path, name: &str) -> Result<Option<PathBuf>> {
fn walk(dir: &Path, name: &str, out: &mut Option<PathBuf>) -> Result<()> {
if out.is_some() {
return Ok(());
}
for entry in fs::read_dir(dir)? {
let entry = entry?;
let p = entry.path();
if entry.file_type()?.is_dir() {
walk(&p, name, out)?;
} else if entry.file_name() == name {
*out = Some(p);
}
}
Ok(())
}
let mut found = None;
walk(root, name, &mut found)?;
Ok(found)
}
pub fn cargo_available() -> bool {
std::process::Command::new("cargo")
.arg("-V")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn asset_url_shape() {
let u = release_asset_url("0.3.4", "marqdo-0.3.4-ext.zip");
assert_eq!(
u,
"https://github.com/cflmy/marqdo/releases/download/v0.3.4/marqdo-0.3.4-ext.zip"
);
}
#[test]
fn triple_known_on_ci_hosts() {
// At least one of these builds is what we ship.
let t = host_target_triple();
if cfg!(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "macos", target_arch = "x86_64"),
)) {
assert!(t.is_some());
}
}
}
+122
View File
@@ -0,0 +1,122 @@
//! CLI arg parsing for Mid M5 (`lib/cli`).
use crate::host::HostContext;
use crate::value::Value;
fn as_text_list(v: &Value) -> Result<Vec<String>, String> {
match v {
Value::List(xs) => {
let mut out = Vec::new();
for x in xs {
match x {
Value::Text(s) => out.push(s.clone()),
_ => return Err("cli.parse: args must be list of text".into()),
}
}
Ok(out)
}
Value::None => Ok(vec![]),
_ => Err("cli.parse: args needs list".into()),
}
}
/// Parse `--key value` / `--key=value` / `--flag` / positionals into a map.
/// Positionals are stored under key `_` as a list.
pub fn parse(ctx: &HostContext, args: Option<&Value>) -> Result<Value, String> {
let argv = match args {
None | Some(Value::None) => ctx.argv.clone(),
Some(v) => as_text_list(v)?,
};
let mut map: Vec<(String, Value)> = Vec::new();
let mut positionals: Vec<Value> = Vec::new();
let mut i = 0usize;
while i < argv.len() {
let a = &argv[i];
if let Some(rest) = a.strip_prefix("--") {
if rest.is_empty() {
// bare `--` — remaining are positionals
i += 1;
while i < argv.len() {
positionals.push(Value::Text(argv[i].clone()));
i += 1;
}
break;
}
if let Some((k, v)) = rest.split_once('=') {
upsert(&mut map, k.to_string(), Value::Text(v.to_string()));
i += 1;
continue;
}
let key = rest.to_string();
if i + 1 < argv.len() && !argv[i + 1].starts_with('-') {
upsert(&mut map, key, Value::Text(argv[i + 1].clone()));
i += 2;
} else {
upsert(&mut map, key, Value::Bool(true));
i += 1;
}
} else if let Some(rest) = a.strip_prefix('-') {
// short flags: -abc → a/b/c True; -k val not special-cased beyond single letter groups
if rest.is_empty() {
positionals.push(Value::Text(a.clone()));
i += 1;
continue;
}
if rest.len() == 1 && i + 1 < argv.len() && !argv[i + 1].starts_with('-') {
upsert(&mut map, rest.to_string(), Value::Text(argv[i + 1].clone()));
i += 2;
} else {
for ch in rest.chars() {
upsert(&mut map, ch.to_string(), Value::Bool(true));
}
i += 1;
}
} else {
positionals.push(Value::Text(a.clone()));
i += 1;
}
}
if !positionals.is_empty() {
upsert(&mut map, "_".into(), Value::List(positionals));
}
Ok(Value::Map(map))
}
fn upsert(map: &mut Vec<(String, Value)>, key: String, value: Value) {
if let Some((_, slot)) = map.iter_mut().find(|(k, _)| *k == key) {
*slot = value;
} else {
map.push((key, value));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_flags() {
let ctx = HostContext::default();
let args = Value::List(vec![
Value::Text("--input".into()),
Value::Text("a.csv".into()),
Value::Text("x".into()),
Value::Text("--verbose".into()),
]);
let m = parse(&ctx, Some(&args)).unwrap();
match m {
Value::Map(entries) => {
assert!(entries
.iter()
.any(|(k, v)| k == "input" && *v == Value::Text("a.csv".into())));
assert!(entries
.iter()
.any(|(k, v)| k == "verbose" && *v == Value::Bool(true)));
assert!(entries
.iter()
.any(|(k, v)| k == "_" && matches!(v, Value::List(_))));
}
_ => panic!("map"),
}
}
}
+130
View File
@@ -385,6 +385,136 @@ pub fn list_last(list: &Value) -> Result<Value, String> {
} }
} }
fn cmp_value(a: &Value, b: &Value) -> std::cmp::Ordering {
use std::cmp::Ordering;
match (a, b) {
(Value::Int(x), Value::Int(y)) => x.cmp(y),
(Value::Num(x), Value::Num(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal),
(Value::Int(x), Value::Num(y)) => (*x as f64).partial_cmp(y).unwrap_or(Ordering::Equal),
(Value::Num(x), Value::Int(y)) => x.partial_cmp(&(*y as f64)).unwrap_or(Ordering::Equal),
(Value::Text(x), Value::Text(y)) => x.cmp(y),
(Value::Bool(x), Value::Bool(y)) => x.cmp(y),
_ => a.as_display().cmp(&b.as_display()),
}
}
pub fn list_sort(list: &Value) -> Result<Value, String> {
match list {
Value::List(xs) => {
let mut out = xs.clone();
out.sort_by(cmp_value);
Ok(Value::List(out))
}
Value::None => Ok(Value::List(vec![])),
_ => Err("list_sort needs list".into()),
}
}
pub fn list_sort_by(list: &Value, key: &Value) -> Result<Value, String> {
let key = match key {
Value::Text(s) => s.as_str(),
_ => return Err("sort_by key needs text".into()),
};
match list {
Value::List(xs) => {
let mut out = xs.clone();
out.sort_by(|a, b| {
let ka = match a {
Value::Map(m) => m
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v)
.unwrap_or(&Value::None),
_ => &Value::None,
};
let kb = match b {
Value::Map(m) => m
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v)
.unwrap_or(&Value::None),
_ => &Value::None,
};
cmp_value(ka, kb)
});
Ok(Value::List(out))
}
Value::None => Ok(Value::List(vec![])),
_ => Err("list_sort_by needs list".into()),
}
}
pub fn list_unique(list: &Value) -> Result<Value, String> {
match list {
Value::List(xs) => {
let mut out = Vec::new();
for x in xs {
if !out.iter().any(|y| y == x) {
out.push(x.clone());
}
}
Ok(Value::List(out))
}
Value::None => Ok(Value::List(vec![])),
_ => Err("list_unique needs list".into()),
}
}
pub fn list_chunk(list: &Value, size: &Value) -> Result<Value, String> {
let n = match size {
Value::Int(i) if *i > 0 => *i as usize,
Value::Int(_) => return Err("chunk size must be positive".into()),
_ => return Err("chunk size needs int".into()),
};
match list {
Value::List(xs) => {
let mut out = Vec::new();
let mut i = 0;
while i < xs.len() {
let end = (i + n).min(xs.len());
out.push(Value::List(xs[i..end].to_vec()));
i = end;
}
Ok(Value::List(out))
}
Value::None => Ok(Value::List(vec![])),
_ => Err("list_chunk needs list".into()),
}
}
pub fn list_zip(a: &Value, b: &Value) -> Result<Value, String> {
let (aa, bb) = match (a, b) {
(Value::List(x), Value::List(y)) => (x, y),
(Value::None, Value::List(y)) => (&Vec::new(), y),
(Value::List(x), Value::None) => (x, &Vec::new()),
(Value::None, Value::None) => return Ok(Value::List(vec![])),
_ => return Err("list_zip needs two lists".into()),
};
let n = aa.len().min(bb.len());
let mut out = Vec::with_capacity(n);
for i in 0..n {
out.push(Value::List(vec![aa[i].clone(), bb[i].clone()]));
}
Ok(Value::List(out))
}
pub fn list_flatten(list: &Value) -> Result<Value, String> {
match list {
Value::List(xs) => {
let mut out = Vec::new();
for x in xs {
match x {
Value::List(inner) => out.extend(inner.iter().cloned()),
other => out.push(other.clone()),
}
}
Ok(Value::List(out))
}
Value::None => Ok(Value::List(vec![])),
_ => Err("list_flatten needs list".into()),
}
}
pub fn collection_clear(value: &Value) -> Result<Value, String> { pub fn collection_clear(value: &Value) -> Result<Value, String> {
match value { match value {
Value::List(_) | Value::None => Ok(Value::List(vec![])), Value::List(_) | Value::None => Ok(Value::List(vec![])),
Loaded 100 of 146 files, more files were not shown because too many files have changed in this diff. Show more