Primitive recursion in Cantor is expressed 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.
primrec f g h combinator, which iterates over natural numbers from 0 to x while threading a running accumulator of previous results. Unlike classical primitive recursion schemes that only expose the immediately preceding result, Cantor’s primrec stores the full history as a right-nested chain of Cantor-encoded pairs, making it possible to reach any prior result by traversing snd projections. This enables functions like Fibonacci — which needs the two most recent values — to be written without any extra bookkeeping.
primrec is part of Cantor’s extended feature set. Programs that use it must include the extended keyword at the top of the file. Without it, the interpreter will reject the primrec keyword.How primrec works
primrec is_base g_base step processes input x by iterating i from 0 to x:
- At each step
i, ifis_base(i) ≠ 0the result for that step isg_base(i). - Otherwise, the result is
step(<i, history>), wherehistoryis the Cantor-encoded right-nested chain of all prior results built by repeatedly pairing each new result with the previous chain. - After the final iteration,
primrecreturns the result computed at stepx.
step, the first previous result is accessed as fst(snd(z)), the second as fst(snd(snd(z))), and so on, where z is the full <i, history> argument.
Factorial
Factorial is the canonical single-step recursion:0! = 1 and n! = n * (n-1)!. Only the immediately preceding result is needed at each step.
k_0 — the constant zero
compair diff k_1 k_1 computes diff(1, 1) = 0. Cantor has no literal 0 constant, so it is synthesised from diff.segon — first previous result
Inside a
step call, the argument is <i, history>. comp fst snd computes fst(snd(z)), which is the result from step i - 1 — the immediately preceding factorial value.is_base — base case guard
compair eq id k_0 checks whether the current index i equals 0. It returns 1 (true) for i = 0 and 0 otherwise.step — recursive multiplication
compair mul fst segon multiplies fst(z) = i by segon(z) = (i-1)!, yielding i * (i-1)! = i!.Fibonacci
Fibonacci requires the two most recent values:fib(n) = fib(n-1) + fib(n-2). This is where Cantor’s history chain becomes essential — prev1 reaches back one step and prev2 reaches back two steps by traversing snd twice.
k_2 — the constant two
compair add k_1 k_1 computes 1 + 1 = 2, synthesising the constant 2 from k_1.is_base — base case for i < 2
compair lt id k_2 returns 1 when the current index i < 2, covering both fib(0) = 0 and fib(1) = 1.prev1 — most recent Fibonacci value
comp fst snd extracts the first element of the history chain: the result from step i - 1.prev_tail — tail of the history chain
comp snd snd drops the first element of history, exposing the rest of the chain starting at step i - 2.prev2 — second-most-recent Fibonacci value
comp fst prev_tail takes the first element of the tail, retrieving the result from step i - 2.step — add the two predecessors
compair add prev1 prev2 sums fib(i-1) and fib(i-2) to produce fib(i).fib(10) = 55: