:input <codec.mjs> [text]. Codecs can only be loaded through :codec load <file.mjs> in the REPL or through the Files explorer.
k is a concise language of relations (functions) over tree-like (JSON-like) documents. Every expression denotes a relation that transforms an input document into an output document.
Content-Addressed: Relations are identified by their canonical content hash (@...) derived directly from their structure. Local names are just temporary aliases used for recursion and readability.
Definitions introduce local aliases for recursion and convenience (terminated by ;):
| Syntax | Description & Example |
|---|---|
name = relExpr; |
Relation Alias: Defines a local alias for a relation expression. e.g. swap = { .y x, .x y }; |
name[P1, ..., Pn] = filter; |
Named Pattern Template: Defines a reusable
pattern template. e.g. bit[] = < {} 0, {} 1 >; orlist[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
relExpr)Functions transforming an input document to an output document:
| Syntax | Description |
|---|---|
{ rel_1 label_1, ..., rel_k label_k } |
Product: Evaluates k relations into record fields (special case: {} evaluates 0 relations, producing empty record {}) |
< rel_1, ..., rel_k > |
Union (Choice): Evaluates relations in order of arity k (tries rel_1; if it fails, tries rel_2, ..., rel_k) |
( rel_1 ... rel_k ) |
Composition: Sequentially composes k relations (special case: () is the 0-ary composition, denoting identity; parentheses may be omitted when non-empty: f g) |
. label |
Field Projection: Extracts field label from a record |
/ tag |
Variant Branch: Extracts payload of variant tag (fails if tag differs) |
| tag |
Variant Constructor: Wraps document into variant tag |
? filterExpr /
? name[...]
|
Pattern Filter: Asserts input matches pattern;
acts as partial identity and inductive witness
(e.g. ? list[X])
|
name / @hash |
Relation Reference: Calls a relation by local alias or canonical content hash |
:engine [wasm|pkir|js] | Display or switch evaluation engine (wasm, pkir, or js; shortcuts: :wasm, :pkir, :js) |
:load [--no-alias] <file> | Load a .k source or .klib library into current state (use --no-alias to import by hash only) |
:pattern <name> |
Display definition of named pattern
<name> (alias :pat)
|
:patterns |
List pattern aliases
(alias :pats)
|
:all-patterns |
List canonical patterns keyed by hash with their aliases
(alias :all-pat)
|
:rel <name> | Display definition of relation <name> |
:rels | List all relation aliases |
:limit [num|none] | Display or set maximum characters for value output |
:max-pat-length [num|none] | Display or set max inline pattern length in :rel (default 45, alias :maxPatLength) |
:codecs | List all loaded format codecs (alias :codec list) |
:codec load <file.mjs> | Load a codec from VFS (or via Files) |
:codec unload <file.mjs> | Unload a loaded codec |
:input <codec.mjs> [text] | Parse input using specified codec (opens dialog if text omitted) |
:klib <file> | Export current state as a .klib binary file |
:ko <file> <expr> | Export executable .ko object using <expr> as main |
:reset | Clear state and reset to initial environment |
:help | Show command reference |
Libraries and examples are bundled in the Virtual File System and can be loaded with :load <file> or from the toolbar. Leading # --lib ... lines are ordinary comments; load prerequisites in dependency order before dependents.
core.k | Standalone bit, Unicode string, and self-hosted pattern schemas (bit[], bits[], list[X], string[], pattern[]) |
lib/generic.k | Algebraic types, binary tries, and string/bit codecs (bool[], order[], opt[V], btrie[V]); load core.k first |
lib/integer.k | Self-contained signed arbitrary-precision integers (int[], plus, minus, Karatsuba times, divmod, div, mod, gcd, lcm) |
lib/ieee.k | Binary64 float types, predicates, negation, and conversions (float64[]); load core.k first |
lib/djson.k | RFC 8259 JSON parser (parse_json) and djson[] operations; load core.k, lib/generic.k, lib/ieee.k first |
Examples/nat.k | Small Peano natural-number arithmetic example (nat[], add, mult) |
Examples/poly.k | Polymorphic list operations indexed by int[]; load lib/integer.k first |
Examples/ieee-arithmetics.k | Binary64 arithmetic returning {result, flags}; load core.k and lib/ieee.k first |
Any raw k expression entered at the prompt is compiled and evaluated on top of the current state (default engine: WebAssembly; switch via :engine). A line ending with ; closes a definition block; end a line with to continue.
{} |ok — Constant relation producing variant ok with empty document payload {}not = < /true |false, /false |true >; — Polymorphic negation via branch projections and ordered choice{} |true not — Compose constant relation {} |true with not (evaluates to {} |false)bool? = ?< {} true, {} false >; — Define a boolean pattern filter relationbit[] = < {} 0, {} 1 >;
— Define a 0-arity bit pattern
? list[bool[]]
— Instantiated pattern filter matching a list of booleans (requires list[X] and bool[] in scope)
not = bool? ?X < /true |false, {} |true > ?X; — Filtered boolean negation: guarded by bool?, defaulting to {} |trueswap = { .y x, .x y }; — Define a product field-swapping relation{ {} |ok x, {} |nil y } swap — Construct a product and pass it through swapEnter | Execute if snippet is complete, otherwise insert newline |
Shift+Enter | Insert newline without executing |
Ctrl+Enter / Cmd+Enter | Force execute whatever is in input |
Tab | Autocomplete commands, aliases, and filenames |
↑ / ↓ | Navigate command history |
Ctrl+L | Clear terminal output |
Esc | Dismiss autocomplete / clear input |