⚠️ This is the static README. The canonical, always-up-to-date README is
README.mbt.md, which runs as executable tests
via moon test README.mbt.md — every code example is verified on every CI run.
本文件仅为静态副本,最新权威版本请查看 README.mbt.md(可执行文档,示例即测试)。
A MoonBit-native pathfinding and graph algorithms library built for rigorous engineering.
A production-grade pathfinding and graph algorithms library for MoonBit,
built to compete with Rust’s pathfinding crate on the axes that matter:
executable proof predicates, executable Markdown documentation,
multi-backend consistency (wasm-gc / native / js), and
reproducible validation scripts.
Ported from
本库 API 哲学 参考自 Rust 社区的
pathfinding crate(v4.15.0,
双许可 MIT OR Apache-2.0)。核心借鉴:
状态: executable runtime predicates exist today; src/proofs is
proof-enabled, and scripts/proof_evidence.ps1 records the current
moon prove result or the exact local toolchain blocker.
The src/proofs/ package encodes post-condition predicates as ordinary
MoonBit functions and tests them in CI. These predicates are the contract
vocabulary that moon prove annotations can reference as the verifier surface
settles. Official MoonBit documentation currently describes moon prove as
experimental, backed by Why3 and SMT solvers.
算法
证明性质
状态
bfs
start/end/edge-validity/minimality/None-witness post-conditions, including bad-witness rejection
✅ runtime-checked
dijkstra
non-negative outputs, weighted path-validity, cost consistency, including bad-witness rejection
Latest local evidence is stored in
docs/verification/latest-proof-evidence.md.
On this machine, runtime proof predicates passed, moon prove --help is
available, and static discharge is blocked because Why3 is not on PATH.
Current benchmark tests and checked-in benches/results/*.json artifacts are
local regression evidence, not a published Rust comparison yet. Native artifacts
record moon bench statistics from @bench.T blocks; smoke artifacts record
end-to-end package timing. Both include machine, backend, input size, command
output, and methodology so regressions can be discussed with concrete data,
while avoiding unsupported speedup claims.
The native guard defaults to a 25% regression tolerance. The smoke guard remains
available with a deliberately loose 50% default because it times end-to-end
moon test -p ... package execution.
OSM 真实路网(planned · Tier-3)
M4 里程碑将加入 OSM 路网基准(见 benches/osm/),同时引入
CH / ALT / JPS 三种前沿算法的可复现对照。任何加速比都必须来自
benches/results/ 中记录的机器、backend、输入和原始计时。
This library is built to compile and run identically on all three MoonBit
backends: wasm-gc, js, and native. Every push to main and every PR
triggers the ci workflow’s 3-backend matrix, which executes the full
test suite (currently 97 blackbox + whitebox cases) on each backend. Any
output divergence — including snapshot mismatches from inspect(..., content=...)
— fails the entire build, giving us a differential test of algorithmic
behaviour across backends for free.
Current benchmark tests are smoke gates, not a published backend comparison.
The repository now includes reproducible smoke artifacts under
benches/results/ with:
Required field
Why it matters
MoonBit version and target backend
Toolchain performance changes over time
Machine / OS / CPU
Makes local numbers interpretable
Input generator and seed
Allows exact reruns
Algorithm, graph size, edge count, query count
Prevents vague benchmark claims
Raw timing and summary statistics
Keeps release notes auditable
Native and smoke regression guards are available through
scripts/benchmark_native_guard.ps1, scripts/benchmark_guard.ps1, and optional
local acceptance:
The native guard is the lower-noise gate; the smoke guard remains useful for
package-level harness regressions.
Target restrictions
Currently no algorithm is backend-restricted. Future additions that rely
on backend-specific features (e.g. SIMD intrinsics on native) will declare
supported_targets in their moon.pkg.json. Template follows design.md §15.1.
Acknowledgements · 致谢
This project stands on the shoulders of three communities:
MoonBit Team & Community — for the toolchain, Discourse feedback, and
the hard work behind moon prove, Markdown-oriented programming, and the
three-backend ecosystem that makes this library possible.
Rust pathfinding crate authors (evenfurther & contributors) — for
the minimalist “successor function” API philosophy that we ported into
MoonBit. Thank you for a decade of principled design in open source.
OSC 2026 mentors & reviewers — for the spec-driven methodology and
continuous, candid feedback during Milestones 0–3.
External code reviewers and discussion participants who shaped this library
(alphabetical, by GitHub handle) are recorded in docs/community/ as the
project grows. Pull requests are warmly welcomed — see
CONTRIBUTING.md.
moonbit-pathfinding
Ported from
本库 API 哲学 参考自 Rust 社区的
pathfindingcrate(v4.15.0, 双许可 MIT OR Apache-2.0)。核心借鉴:fn(N) -> Array[N]或fn(N) -> Array[(N, W)]定义邻居关系。N : Eq + Hash足够,无需Ord约束。Option[(Array[N], W)]表示”可能无解的带权最短路”。但所有算法实现均独立派生自原始论文,不是逐行移植。本库在此基础上原 创贡献:
moon proveupgrade path.moon test README.mbt.md, so examples are compiled and snapshot-checked instead of drifting.Quick Start
1. 安装依赖
在你的 MoonBit 项目根目录执行:
在需要调用算法的包的
moon.pkg里声明导入:2. Dijkstra 最短路 · 5 节点小图
考虑下面的有向带权图 (节点
A..E对应索引0..4):三条候选路径:
1 + 2 + 14 + 11 + 3 + 2完整可运行示例 (
cmd/main/main.mbt):运行:
预期输出:
即最短路径为
A → B → C → D, 总代价4,与上方表格第 1 行结果吻合。Example Workflows
The repository ships three runnable workflows that exercise different user stories instead of isolated snippets:
moon run examples\maze_solvermoon run examples\network_routingmoon run examples\eight_puzzleVerify all example outputs with checked markers:
Latest evidence:
docs/examples/latest-examples-run.mdanddocs/examples/latest-examples-run.json.Release Readiness
Package metadata is checked against mooncakes.io publishing expectations: SemVer version, SPDX license, repository, homepage, keywords, README, changelog, and package artifact generation.
Latest evidence:
docs/release/latest-release-readiness.mdanddocs/release/latest-release-readiness.json. The current local guard passes with one environment warning:moon publish --dry-runneeds mooncakes credentials frommoon loginor CI secrets.Algorithm Catalog
当前已落地 30 种经典图/路径算法 与 3 种实验性前沿算法。 CH / JPS / ALT 已有源码和测试,仍需要真实路网基准、论文到代码追踪和性能调优后 再升级为稳定 API。
src/unweighted/bfs.mbtsrc/directed/dfs.mbtsrc/directed/dijkstra.mbtsrc/directed/astar.mbtsrc/directed/bellman_ford.mbtsrc/directed/floyd_warshall.mbtsrc/undirected/kruskal.mbtsrc/undirected/connected_components.mbtsrc/directed/bidirectional_bfs.mbtsrc/directed/topo_sort.mbtsrc/directed/tarjan_scc.mbtsrc/directed/edmonds_karp.mbtsrc/directed/ida_star.mbtsrc/directed/yen.mbtsrc/undirected/kuhn_munkres.mbtsrc/undirected/prim.mbtsrc/directed/dag_shortest_path.mbtsrc/undirected/bridges.mbtsrc/directed/bidirectional_dijkstra.mbtsrc/directed/dijkstra_all.mbtsrc/unweighted/bfs_all.mbtsrc/directed/bellman_ford_paths.mbtsrc/directed/floyd_warshall_paths.mbtsrc/directed/johnson.mbtsrc/directed/dinic.mbtsrc/directed/min_cut.mbtsrc/directed/min_cost_flow.mbtsrc/undirected/hopcroft_karp.mbtsrc/directed/eulerian.mbtsrc/directed/condensation.mbtsrc/advanced/ch.mbtsrc/advanced/jps.mbtsrc/advanced/alt.mbtPlayground
The current repository ships examples and executable documentation, but not a browser playground yet. The planned playground acceptance target is:
moon build --target wasm-gc产出 ≤ 100 KB 的.wasm模块https://Suquster.github.io/moonbit-pathfinding/对应需求: R16 (WASM Playground) · R26 (实时 JPS Playground 杀手锏)。
Formal verification
The
src/proofs/package encodes post-condition predicates as ordinary MoonBit functions and tests them in CI. These predicates are the contract vocabulary thatmoon proveannotations can reference as the verifier surface settles. Official MoonBit documentation currently describesmoon proveas experimental, backed by Why3 and SMT solvers.bfsdijkstramoon provestatic dischargeRun the current evidence chain with:
Latest local evidence is stored in
docs/verification/latest-proof-evidence.md. On this machine, runtime proof predicates passed,moon prove --helpis available, and static discharge is blocked because Why3 is not onPATH.对应需求: R8 (形式化证明撒手锏) · R25 (答辩故事张力)。
Benchmarks
moonbit-pathfinding以benches/目录承载可复现、可 CI 回归的性能证据:moon testsmoke guards 验证工作负载正确性,moon bench原生@bench.T块记录更低噪声的算法级时间。当前基准覆盖 4 个 MVP 算法:benches/bfs_bench/bfs_bench.mbtbenches/dijkstra_bench/dijkstra_bench.mbtbenches/astar_bench/astar_bench.mbtbenches/kruskal_bench/kruskal_bench.mbt运行
每个基准文件都有
test "smoke: ..."和test "bench: ..." (b : @bench.T)两层入口:前者进入普通测试,后者由moon bench采样。scripts/benchmark_native.ps1会生成算法级结果:benches/results/latest-native.md与benches/results/latest-native.json。scripts/benchmark_native_guard.ps1会把当前 native run 写入_build/native-benchmark-guard/临时目录,并和 checked-in baseline 比较 medianmoon benchmean timing,生成benches/results/latest-native-guard.md与benches/results/latest-native-guard.json。scripts/benchmark_smoke.ps1会额外生成可审计结果:benches/results/latest-smoke.md与benches/results/latest-smoke.json。scripts/benchmark_guard.ps1会把当前 smoke run 写入_build/benchmark-guard/临时目录,并和 checked-in baseline 比较 median,生成benches/results/latest-guard.md与benches/results/latest-guard.json。对标 Rust
pathfindingcrateCurrent benchmark tests and checked-in
benches/results/*.jsonartifacts are local regression evidence, not a published Rust comparison yet. Native artifacts recordmoon benchstatistics from@bench.Tblocks; smoke artifacts record end-to-end package timing. Both include machine, backend, input size, command output, and methodology so regressions can be discussed with concrete data, while avoiding unsupported speedup claims.The native guard defaults to a 25% regression tolerance. The smoke guard remains available with a deliberately loose 50% default because it times end-to-end
moon test -p ...package execution.OSM 真实路网(planned · Tier-3)
M4 里程碑将加入 OSM 路网基准(见
benches/osm/),同时引入 CH / ALT / JPS 三种前沿算法的可复现对照。任何加速比都必须来自benches/results/中记录的机器、backend、输入和原始计时。Multi-backend consistency · 三后端一致性
This library is built to compile and run identically on all three MoonBit backends:
wasm-gc,js, andnative. Every push tomainand every PR triggers theciworkflow’s 3-backend matrix, which executes the full test suite (currently 97 blackbox + whitebox cases) on each backend. Any output divergence — including snapshot mismatches frominspect(..., content=...)— fails the entire build, giving us a differential test of algorithmic behaviour across backends for free.Backend × Algorithm matrix
Performance evidence
Current benchmark tests are smoke gates, not a published backend comparison. The repository now includes reproducible smoke artifacts under
benches/results/with:Native and smoke regression guards are available through
scripts/benchmark_native_guard.ps1,scripts/benchmark_guard.ps1, and optional local acceptance:The native guard is the lower-noise gate; the smoke guard remains useful for package-level harness regressions.
Target restrictions
Currently no algorithm is backend-restricted. Future additions that rely on backend-specific features (e.g. SIMD intrinsics on native) will declare
supported_targetsin theirmoon.pkg.json. Template follows design.md §15.1.Acknowledgements · 致谢
This project stands on the shoulders of three communities:
moon prove, Markdown-oriented programming, and the three-backend ecosystem that makes this library possible.pathfindingcrate authors (evenfurther & contributors) — for the minimalist “successor function” API philosophy that we ported into MoonBit. Thank you for a decade of principled design in open source.External code reviewers and discussion participants who shaped this library (alphabetical, by GitHub handle) are recorded in
docs/community/as the project grows. Pull requests are warmly welcomed — see CONTRIBUTING.md.For code agents and scripted integrations, see AI_AGENT_USAGE.md.
License
Apache-2.0 © 2026 Suquster. See LICENSE.