A Cantor program is a plain UTF-8 text file with theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/felipenugo/cantor-interpreter/llms.txt
Use this file to discover all available pages before exploring further.
.cantor extension. The grammar is defined in src/cantor.g4 using ANTLR4; the parser and lexer are generated from that file. This page walks through every grammar rule, every keyword, and every lexical convention the language uses.
Complete grammar
The following is the full ANTLR4 grammar for Cantor, sourced fromsrc/cantor.g4 (lexer token rules condensed to single lines for readability):
Grammar rule reference
program
- Exactly one
mainDirective - An optional
extendedDirective - Zero or more
importDirectives - Zero or more
functionDefs
mainDirective
IDENTIFIER must be the name of a function defined in this file or in one of its imports, or a built-in function name.
Example:
extendedDirective
extended on its own line enables extended mode for the entire program. Extended mode is required to use compair and primrec. This directive has no arguments.
Example:
importDirective
<IDENTIFIER>.cantor in the same directory as the running program. Imported function definitions are merged into the current interpreter state. Multiple import directives may appear; each is processed in order.
Example:
functionDef
IDENTIFIER— the function’s nameDOC— a documentation block enclosed in[and]body— one combinator expression
body
| Combinator | Argument count | Meaning |
|---|---|---|
pair f g | 2 | Cantor-pair the outputs of f and g |
comp f g | 2 | Compose: f(g(x)) |
compair f g h | 3 | Compose after pairing: f(<g(x).h(x)>) — extended only |
mu f | 1 | Minimization over f |
primrec f g h | 3 | Primitive recursion — extended only |
Keywords
The following words are reserved and cannot be used as function names:Lexical conventions
IDENTIFIER
anterior, k_1, diff_xy, test_quotient.
DOC — documentation blocks
]. The interpreter parses but ignores the content of DOC tokens — they exist solely for inline documentation.
DOC blocks are required by the grammar: every functionDef must have one. If you have nothing meaningful to write, use [] as a minimal placeholder. The interpreter does not evaluate or validate the text inside brackets.Comments
# character starts a line comment. Everything from # to the end of the line is discarded by the lexer. Comments may appear anywhere whitespace is allowed.
Whitespace
Annotated example program
The following program fromtests/programs/phase1-core/anterior.cantor computes the predecessor function — anterior(0) = 0, anterior(n) = n − 1 — using only core combinators and built-ins:
How the predecessor works step by step
How the predecessor works step by step
Given input
When
x:aparella_amb_1appliespair id k_1: bothid(x) = xandk_1(x) = 1are computed, then Cantor-paired to give<x.1>.anteriorappliescomp diff aparella_amb_1: firstaparella_amb_1(x)produces<x.1>, thendiff(<x.1>)returnsmax(0, x − 1).
x = 0: diff(<0.1>) = max(0, 0 − 1) = 0. ✓When
x = 5: diff(<5.1>) = max(0, 5 − 1) = 4. ✓