Skip to main content

Documentation 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 is a minimalist interpreted language designed to express computation purely over the natural numbers. Every value in a Cantor program is a natural number (ℕ), every function maps ℕ → ℕ, and all multi-argument computation is encoded through Cantor pairing. The interpreter is written in Python and uses an ANTLR4-generated parser built from a formal grammar.

Mathematical foundations

All values in Cantor are elements of ℕ (0, 1, 2, …). When a computation requires two inputs — for example, addition of x and y — both numbers are first encoded into a single natural number using the Cantor pairing function:
π(x, y) = ((x + y) × (x + y + 1)) / 2 + y
This encoding is written <x.y> throughout this documentation. The pairing is reversible: built-in functions fst and snd decode the two components. Larger tuples are encoded by nesting pairs from right to left, so a triple <a, b, c> is represented as <a.<b.c>>. Because the entire value domain is ℕ and all functions operate on single natural numbers, the language achieves multi-argument computation without any special syntactic support for tuples or lists.

Program structure

A Cantor program is a plain-text .cantor file that follows a strict top-down order:
1

main directive

Declares which function to run as the entry point, e.g. main factorial.
2

extended directive (optional)

The bare keyword extended enables the compair and primrec combinators. Without it those combinators raise an error at runtime.
3

import directives (zero or more)

Each import IDENTIFIER loads <IDENTIFIER>.cantor from the same directory and brings all its function definitions into scope.
4

function definitions (zero or more)

Each define name [doc] body gives a name to one combinator expression. All named functions are available to any definition that follows — or that was imported before.

The five combinators

Every non-trivial Cantor function is built from exactly one of five combinators. No other control flow exists.
CombinatorSyntaxSemantics
compcomp f gf(g(x)) — function composition
pairpair f g<f(x).g(x)> — Cantor-encode two results
compaircompair f g hf(<g(x).h(x)>) — extended shortcut for comp f (pair g h)
mumu fsmallest k ≥ 0 where f(<x.k>) ≠ 0 — minimization
primrecprimrec f g hprimitive recursion from 0 to x
compair and primrec are only available in extended mode.

The seven built-in functions

Built-in functions are always in scope; they do not need to be defined or imported.
NameSignatureDescription
k_1ℕ → ℕAlways returns 1
idℕ → ℕReturns its input unchanged
add<x.y> → ℕReturns x + y
mul<x.y> → ℕReturns x × y
diff<x.y> → ℕReturns max(0, x − y) (monus)
fst<x.y> → ℕReturns x
snd<x.y> → ℕReturns y

Computational power

Cantor with comp, pair, and the built-ins covers a rich class of functions. Adding mu (unbounded minimization) makes the language Turing-complete: any computable function over ℕ can be expressed. The primrec combinator provides a structured alternative to mu for functions whose recursion depth is bounded by the input.
Because mu has no bound and no timeout, a program that calls mu on a predicate that never becomes true will run forever. This is an expected property of any Turing-complete language.

Language phases

The test suite is organized into phases that reflect the progressive expressive power of the language:
PhaseFeatures demonstrated
Phase 1 — Corecomp, pair, and the seven built-ins; programs like predecessor and addition
Phase 2 — ImportsSplitting programs across multiple .cantor files with import
Phase 3 — Extendedcompair for concise relational functions (eq, lt, gt)
Phase 4 — Minimizationmu for division and modulo by linear search
Phase 5 — Conditionals & primrecprimrec and conditional logic for factorial, Fibonacci, etc.

Explore the language

Syntax

Grammar rules, keywords, identifiers, comments, and a fully annotated example program.

Operations

Detailed reference for all five combinators with semantics and worked examples.

Imports

How to split programs across files and how import resolution works.

Extended Mode

What the extended keyword unlocks and when you need it.

Build docs developers (and LLMs) love