@fraczak/k

中文文档
8.8.0发布于 2026/11/13ISCESM + CJS近 30 天下载 3034

k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain

原始使用文档
文档目录6 节
  1. A small example
  2. Run from source
  3. Libraries and examples
  4. JavaScript API
  5. Development
  6. Release checks

k

k is a small language for describing algebraic data shapes and composing transformations over them. Programs are first-order partial functions, called relations, built from products, tagged unions, and composition. Named patterns describe reusable schemas; the compiler derives structural input/output contracts.

Try k in the browser · Getting started · Documentation

The paper on Zenodo introduces the language. Its LaTeX source and PDF are in this repository.

A small example

This relation swaps two fields:

swap = { .x y, .y x };
{ {} |hello x, {} |world y } swap

Each field expression receives the same input. The result has world under x and hello under y. Relations can be undefined for some inputs, and ordered choice <f, g> tries g when f returns undefined.

All values are built from products and variants. Integers and strings are library-defined structures, with codecs for familiar external representations.

Run from source

Use Node.js 22+ for development and the browser tests. The published runtime supports Node.js 18+.

npm install
node repl.mjs

npm install generates the parsers. From another terminal at the repository root, try a complete pipeline:

node codecs/unit.mjs --parse | node k.mjs '{} |ok' | node codecs/k-print.mjs

It prints "ok". The pipeline carries a binary schema followed by a payload; the codec commands handle the text boundaries.

To expose the commands on your path, run npm link. The equivalent pipeline is:

k-unit --parse | k '{} |ok' | k-print

Build the standalone browser REPL with npm run build:repl-html, then open repl.html. See the walkthrough for recursive relations, libraries, and codecs.

Libraries and examples

FilePurpose
core.kStandalone bit, Unicode string, and self-hosted pattern schemas (bit[], bits[], list[X], string[], pattern[])
lib/generic.kAlgebraic types, binary tries, and string/bit codecs (bool[], order[], opt[V], btrie[V], string_to_bits, bits_to_string)
lib/integer.kSigned arbitrary-precision integer arithmetic (int[], nat, int, plus, minus, times via Karatsuba, divmod, div, mod, gcd, lcm)
lib/ieee.kBinary64 types, predicates, negation, and rational conversions (float64[], negate_float64, bits_to_float64, ratio_to_float64)
lib/djson.kRFC 8259 JSON parser (parse_json) and dynamic JSON operations (djson[], get, nth, set, del, push, keys, values)
Examples/nat.kSmall Peano arithmetic example
Examples/poly.kPolymorphic list operations indexed by int[]; load lib/integer.k first
Examples/ieee-arithmetics.kBinary64 arithmetic returning {result, flags}; load core.k and lib/ieee.k first

Load libraries explicitly with :load in the REPL. Leading # --lib <dependency> --use <specs> lines in source files are ordinary human-readable comments documenting expected dependencies. On the CLI, --lib <dependency.klib> makes library definitions available in memory by content hash (@<hash>), while --use explicitly brings requested friendly names or aliases into compilation scope. See file formats and tools.

JavaScript API

import k from "@fraczak/k";

const fn = k.compile("{} |ok");
const result = fn(k.fromObject({}));
console.log(result.tag); // "ok"

Compiled functions return a Value or undefined. A Value carries its structural pattern as well as its payload.

Development

npm test
npm run test:backends
npm --prefix k-vscode test

See tests/README.md for suite boundaries and optional tools. Grammar changes require npm run prepare; generated parsers are not edited by hand.

The compiler guide maps the implementation and execution layers. WebAssembly, LLVM, and the VS Code extension have their own usage instructions.

k is experimental: syntax, diagnostics, libraries, and artifact formats may change. Release history is in CHANGELOG.md.

Release checks

GitHub CI runs the main, backend, and editor suites, then installs the packed archive with production dependencies and checks its API and commands on Node.js 22 and 18. Run npm run test:package -- /path/to/installed/@fraczak/k to repeat the package check. npm publish runs the main suite before publishing.

版本与发布记录

@fraczak/k 共发布 86 个版本,首次发布于 2022/11/28;过去 12 个月发布了 47 个版本。

版本发布日期距今
8.8.0latest2026/11/132 天前
8.7.12026/11/096 天前
8.7.02026/11/087 天前
8.6.12026/11/0510 天前
8.6.02026/11/0412 天前
8.5.12026/11/0114 天前

关于 @fraczak/k 的常见问题

@fraczak/k 是什么?

k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain

如何安装 @fraczak/k?

在项目目录运行 npm install @fraczak/k,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 k、k-vm、k-int、k-kir、k-ieee、k-json、k-llvm、k-repl、k-show、k-unit、k-utf8、k-wasm,可以用 npx k 直接运行。

@fraczak/k 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fraczak__k 是否存在。

@fraczak/k 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fraczak/k 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fraczak/k 可以商用吗?

@fraczak/k 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fraczak/k 的下载量有多少?

根据 npm 官方下载统计,@fraczak/k 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,034 次,日均约 101 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fraczak/k 还在维护吗?

最新版本 8.8.0 发布于 2026/11/13(2 天前);至今共发布 86 个版本,其中过去 12 个月发布了 47 个;首次发布于 2022/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。